Help with routing (/:name/:named_param) - cakephp

Im stuck in cakephp routing.
I want to redirect "/coches/mustang-100/" to "cars" model with "display" view.
I'm trying with this, but it redirects me to "cars" "index" view:
Router::connect('/:coches/:modelo_coche/',
array('controller' => 'cars', 'action' => 'display'),
array('pass' => array('modelo_coche'),'coches' => 'coches')
);
I want to: www.myweb.com/coches/mustang-100/

I hope I got your needs right, please try if this one matches what you want:
Router::connect('/coches/:modelo_coche', array('controller' => 'cars', 'action' => 'display'), array('pass' => array('modelo_coche')));

This probably isn't the answer but I don't have the rep to comment on your question yet.
But, can you put a trailing slash on the end of a route like that?
I've never seen routing done with slashes at the end so this could be the problem here.

Related

how to link to a specific point on a page with cakephp

I'm trying to get a link in cakephp to point to a specific place on another page but what I imagined would work doesn't.
I'm using
<a name="Telstra"></a>
Telstra
Can anyone tell me the correct way?
You need to use the built-in link function of CakePHP. try to use this code.
<?php echo $this->Html->link('NameOfLink', array('controller' => 'ControllerName', 'action' => 'FunctionName/#Telstra')); ?>
See this url from cakephp docs:
http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::url
For creating link only
echo $this->Html->url(array(
"controller" => "posts",
"action" => "search",
"#" => "first"
));
and in your case for creating the link with anchor tag,
echo $this->Html->link('Telestra',array(
"controller" => "sponsors",
"action" => "index",
"#" => "Telstra"
));
If you want to load on a specific part of the page you must use ID on the element of your target rather than Name.
Example:
//Your target element on a page
<a name="Telstra"></a>
//URL that will redirect you to your target element.
Telstra
Or might as well code it the cakePhp style. :)
//URL
<?php echo $this->Html->link('Telstra', array('controller' => 'YourController', 'action' => 'YourFUnction', '#TargetElement'));
//Target Element
<a name="Telstra"></a>
Good Luck!
you need put a route in the folde of configuration /app/config/routes.php:
Router::connect('/index', array('controller' => 'yourController', 'action' => 'index'));
or
Router::connect('/index/*', array('controller' => 'yourController', 'action' => 'index'));
I think the parameter you're meaning to put is 'id' and not 'name' and I think you can just put #idName instead of the whole link.
For example:
<a id="Telstra"></a>
Telstra
For more information you can check here: http://www.w3schools.com/html/html_links.asp

CakePhp Routes with Controller wildcard

I'd like reroute all domainname.com/affiliateXYZ visits to an AffController reroute action. But how?
The route element substring of 'affiliate' will be consistent and substring 'XYZ' will vary.
I would've preferred project requirements that had allowed domainname.com/aff/reroute/XYX (or domainname.com/aff/reroute/CigarKing) in the controller/action/parameter format, but alas.. tis not the case.
Initially I tried the following unsuccessfully:
Router::connect('/affiliate*', array('controller' => 'aff', 'action' => 'reroute'));
Router::connect('/affiliate**', array('controller' => 'aff', 'action' => 'reroute'));
Router::connect('/:controller*', array('controller' => 'aff', 'action' => 'reroute'), array('controller'=>'(affiliate)');
Once in the controller reroute action I will explode the affiliateXYZ into just 'XYZ' and then proceed with things I know how to do. My issue is routing to the controller with affiliateXYZ as the first routing element and at the same time not messing up current functionality.
I'd used Router::connect('/:affiliate', array('controller' => 'aff', 'action' => 'reroute'),array('pass'=>array('affiliate'))); to get to the info into the controller, until I saw the negative impact on preexisting controllers (I assume because I didn't explicitly list all other controllers, which I don't think I should have to do.).
How to route w/prefix ( more accurately a substring of 'affiliate' )?
I know prefix is the wrong terminology.
Thx, in advance.
You are looking for regular expression matching, with the help of this functionality you can define how specific route elements are matched.
This option is available for the third parameter of Router::connect().
You define it in element => regex format:
Router::connect(
'/:affilate',
array(
'controller' => 'aff',
'action' => 'reroute'
),
array(
'pass' => array('affilate'),
'affilate' => 'affilate[^/]+'
)
);
This would match any URL that starts with affilate, followd by 1 or more characters that are not a /.
See also http://book.cakephp.org/2.0/en/development/routing.html#route-elements

Pass variable into url with CakePHP

on my website I use Cake PHP and I have an url like this for an article:
/article/testx?name=Stefy
I would like to do a kind of "mod rewrite" and have an url like this:
/name/Stefy
I tried to do it from routes.php but I don't know how to do.
I checked on CakePHP website about "pass" function in array and other topics here on StackOverflow but I can't find a solution, probably because I am a beginner with CakePHP.
Can you help me please?
I thought I should something like this:
Router::connect('/name/:id', array('controller' => 'articoli', 'action' => 'display','testx?name=$id') );
but of course it doesn't work. I think I have to use "pass" in the routes.php
Can you help me?
Thank you!
yeah, you kinda do have to use "pass":
Router::connect('/name/:id',
array('controller' => 'articoli', 'action' => 'display'),
array('pass' => array('id')));
you can than generate links like this:
$this->Html->link('Title', array('action' => 'display', 'id' => 1));
Router::connect('/:slug',array('controller' => 'salons', 'action' => 'details'), array('pass' => array('slug')));

Cake PHP route controller/action to other controller/action

How can I make cakePHP go to ef_users/logout when I click the users/logout link?
Thanks in advance
EDIT
This doesn't seem to work
Router::connect('/users/:action/*', array('controller' => 'ef_users', 'action' => 'logout'));
A couple of options:
Make the link point to the correct place in the first place
Use routing: http://book.cakephp.org/view/945/Routes-Configuration
Redirect the user with $this->redirect( url ): http://book.cakephp.org/view/982/redirect
If in doubt, just use as precise route as possible and put it near the top of the list
Router::connect('/users/logout', array('controller' => 'ef_users', 'action' => 'logout'));
You might also consider using logoutRedirect which will still log the user out using the standard CakePHP logout function then redirect the user to your ef_users logout action.
$this->Auth->logoutRedirect = array('controller' => 'ef_users', 'action' => 'logout');
More information at: http://book.cakephp.org/view/1271/logoutRedirect

how to use i18n for one language cakephp

I need to launch a web app in spanish for the moment and I need to translate the app...
I already modified the default.po and added configure::write('Config.language', 'es') to the core.php...
what now? I don't want to add routing rigth now. Any suggestions?
PD: did everything as it is in the manual and ##$%^&%$## I cant get it to work
i18n is a tricky one to get your head around. If you're producing a website that will just be in Spanish, there is no need to use it, but I do use po messages as a matter of course, just in case.
There is a component that will help you a lot: http://bakery.cakephp.org/articles/p0windah/2007/09/12/p28n-the-top-to-bottom-persistent-internationalization-tutorial
There also used to be a script that would allow translation of slugs so that SEO would direct you to the right language. Last time I looked, it had vanished, but I'll try to piece it together for you.
For the moment, this is what I used in router.php
//route to switch locale
Router::connect('/lang/*', array('controller' => 'p28n', 'action' => 'change'));
//forgiving routes that allow users to change the lang of any page
Router::connect('/eng?/*', array(
'controller' => "p28n",
'action' => "shuntRequest",
'lang' => 'en-gb'
));
Router::connect('/ca?/*', array(
'controller' => "p28n",
'action' => "shuntRequest",
'lang' => 'cat'
));
Router::connect('/es?/*', array(
'controller' => "p28n",
'action' => "shuntRequest",
'lang' => 'es_es'
));
I'll dig around for the url translation, but it may take a while....

Resources