I have the following two routes which make the url /posts/recent show page 1 of the recent filter on index method of my posts controller and also allow paging like: /posts/recent/page:2 by using the * on the next route. As you can see I call page 1 on the first route so that I don't get duplicate urls for page 1.
Router::connect('/posts/recent', array('controller'=>'posts','action'=>'index','filter'=>'recent', 'page' => 1), array('pass'=>array('filter')));
Router::connect('/posts/recent/*', array(
'controller' => 'posts', 'action' => 'index', 'filter'=>'recent'), array(
'named' =>array('page' => '[\d]+'),
'pass'=>array('filter')
)
);
However I would like to make it so that named params do this instead:
/posts/recent/page/2 but how do I do it?
I've looked around the docs but don't seem to see anything about doing this...
Also is it possible to turn off named parameters in favour of query strings?
I'm using CakePHP 2.1 if it matters.
Perhaps with Router::connectNamed()?
http://book.cakephp.org/2.0/en/development/routing.html
Related
I am facing some problems in routing under cakephp
there are three actions in my controller
they are as below
www.example.com/photos/newphotos
www.example.com/photos/random
www.example.com/photos/popular
I want them as
www.example.com/newphotos
www.example.com/random
www.example.com/popular
so i routes file under config file I wrote as
Router::connect('/:newphotos', array('controller' => 'photos', 'action' => 'newphotos'));
Router::connect('/:popular', array('controller' => 'photos', 'action' => 'popular'));
Router::connect('/:random', array('controller' => 'photos', 'action' => 'random'));
its working fine when I hit the url
www.example.com/newphotos
but when I hit url www.example.com/random or www.example.com/popular , its again point to action newphotos.
so how can I solve it
(In other words I need to remove controller name "photos" from url for every action)
Many thanks
Why not remove the : from the routes?
If you want to stick with /: paths, then you would need to supply a third parameter to Router::connect() in which to specify patterns for the added options. That is, if you have /:popular as the first parameter, you would need array('popular' => 'popular') as the third parameter, making the rule look like:
Router::connect('/:popular', array('controller' => 'photos', 'action' => 'popular'), array('popular' => 'popular'));
This means that :popular will be matched against the given regex, that is the literal 'popular'. See CakePHP's docs for more info.
Nevertheless, this is useless and silly, so you should stick with paths without colons.
Just delete the colon from the first parameter. They are kind of "capturing variables", so now you basically are routing all / with some parameters to photos/newphotos, and the parameters being captured to :newphotos. As it always will match the first route, then it will not look for the others.
I currently have a Zend Framework route defined as such:
$route = new Zend_Controller_Router_Route('brand/:brand_name/series/:page',
array('controller' => 'brand',
'action' => 'series',
'page'=>'1'));
$router->addRoute('Brand Series', $route);
I'm trying to adapt this route so that the page parameter only catches numbers, so that I can add another route that uses words in the same place without the two conflicting, something like:
brand/:brand_name/series/:series_name/:page
I figured I would step along with the examples in the ZF documentation here. The very first step would be to change the route to something like this:
$route = new Zend_Controller_Router_Route_Regex('brand/:brand_name/series/(\d+)',
array('controller' => 'brand',
'action' => 'series'));
However, this small change causes routes that matched perfectly before, like /brand/johnnycupcakes/series/2 to fail, telling me Action "johnnycupcakes" does not exist and was not trapped in __call(). And in the stack trace I see:
'controller' => 'brand',
'action' => 'johnnycupcakes',
'series' => '2',
'module' => 'default'
In fact, even if I leave the route and default parameters exactly the same as in the first example, and simply change the class to Router_Route_Regex, I get the same error.
I know that the error isn't a routing conflict, because I haven't added the route that would have conflicted. Plus, it appears that it's attempting to match to the standard route. I'm testing this on version 1.11, so my version should be perfectly compatible with the code in the example.
As far as I can tell, the regex route is simply not matching, despite that it very clearly fits. Why could this possibly be failing?
EDIT:
I omitted the addRoute from the question the first time. I always had it in the code, that's not the issue.
What you need is to name a captured numeric parameter by adding third argument to Zend_Controller_Router_Route_Regex:
$route = new Zend_Controller_Router_Route_Regex(
'brand/:brand_name/series/(\d+)',
array(
'controller' => 'brand',
'action' => 'series'
),
array(
1 => 'series', // name the parameter captured by (\d+)
)
);
The second array may have keys and values in opposite relation 'series' => 1 and it will still work. Check more in ZF manual on regex routes
I have a problem with the url rewriting.
The problem that I am facing is that currently our urls are like this:
http://www.xyz.com/sc_users/index
I dont want the controller name to be shown in that url.
Is there a way to achieve that??
First of all thank your guys..
Like I have 8 controllers I dont want the controller name to be shown in my url....this is what I want..
To be more precise no controller name in my url
You can define custom routes in app/config/routes.php. You'll find the all about routes in the CakePHP cookbook under Defining Routes. For example, a custom route can look like this:
Router::connect(
'/the_url_you_want_to_use/*', array('controller' => 'sc_users', 'action' => 'index')
);
You need to read up about CakePHP routing, look at the examples under 'defining routes'. Update your question with what you would actually like your URLs to look like and we will be able to help you more effectively.
That's simple :
there is a file called routes.php in /config directory :
You can do url rewriting stuff there like this :
Router::connect('/pages/*', array('controller' => 'cmsPage', 'action' => 'render'));
You can pass more complicated variables to your controller :
Router::connect('/:id-:lang-:profile-:firstName-:lastName-:profile.htm',
array('controller' => 'profiles','action' => 'view'),
array('id'=>'[0-9]*', 'lang'=>'fr','firstName'=>'[^-]*','lastNAme'=>'[^-]*','profile' => $util->keywords['profiles'][0]['fr'], 'pass' => array('id', 'lang'),'profile' => $util->keywords2['profiles'][0]['en'])
)
;
As you can see in the last example I have passed 2 parameters to the controller through 'pass' => array('id', 'lang')
I have created a cakephp app. I have urls looks like
www.mysite.com/products/search/hardware
It loads fine. But I want urls that looks like
www.mysite.com/hardware
Can this is achieved by setting route connect
I appreciate any help.
Thanks.
Yes, you can use a route.
Router::connect(
'/hardware',
array('controller' => 'products', 'action' => 'search', 'hardware')
);
For a more general solution (any category name routing to products/search) see http://book.cakephp.org/view/948/Defining-Routes
You could add something like
Router::connect('/hardware',
array('controller' => 'products', 'action' => 'search'),
array('pass' => array('search'), 'search' => 'hardware'));
in your routes.php file, but then you would have to do it for each searchable item.
The problem you will be facing if you want something automatic, is that you need a way to differentiate your searchable products from every other model you have. So maybe you should settle for another type of URL like
www.mysite.com/products/hardware
or
www.mysite.com/s/hardware
and use the appropriate routes accordingly.
How about creating hardware_controller in your controllers folder.
I've got a question considering Zend_Controller_Router. I'm using a a modular-structure in my application. The application is built upon Zend-Framework. The normal Routes are like this:
/modulename/actionname/
Since I always use an IndexController within my modules, it's not necessary to provide it in the url. Now I am able to append params like this:
/modulename/actionname/paramkey/paramvalue/paramkey/paramvalue
So this is normal in ZF, I guess. But in some cases I don't want to provide a paramkey within the url. For example I want a blog-title to be shown within the url. Of course this is intended for SEO:
/blog/show/id/6/this-is-the-blog-title
In this case, blog is the module, show is the action. id is a paramkey and 6 is the id of the blogpost I want to show. this-is-the-blog-title is of course the headline of the blogpost with the id 6. The problem is, that if I do use the assemble()-method of the router like this:
assemble(array('module' =>'blog',
'action' => 'show',
'id' => $row['blog_id'],
$row['blog_headline_de'] . '.html'));
the url results in:
blog/show/id/6/0/this-is-the-blog-title.html
As you can see a 0 is inserted as a key. But I want this 0 to be omitted. I tried this by using the blogtitle as key, like this:
assemble(array('module' =>'blog',
'action' => 'show',
'id' => $row['blog_id'],
$row['blog_headline_de'] . '.html' => ''));
This results in:
blog/show/id/6/this-is-the-blog-title.html/
Now the 0 is omitted, but I've got the slash at the end.
Do you have any solution to get an url without 0 as key and without an ending slash?
Regards,
Alex
You might want to use a custom route for this:
$router->addRoute(
'blogentry',
new Zend_Controller_Router_Route('blog/show/:id/:title',
array('controller' => 'index', 'module' => 'blog'
'action' => 'info'))
);
And call your assemble with the route as second parameter. See the Zend_Controller_Router_Route section of the documentation for more details (they even provide examples with assemble).
Or in a more general way:
$router->addRoute(
'generalseo',
new Zend_Controller_Router_Route(':module/:action/:id/:title',
array('controller' => 'index'))
);