Kohana v3 routes - multiple optional parameters - php

I'm setting up a search system which has urls eg. all parameters are optional and there are 15 possible params in total
http://example.com/search/key1-value/key2-value/key3-value/key13-value/key15-value
Is there a better way to set up the route than this?
Route::set('search', 'search(/<param1>(/<param2>(/<param3>(/<param4>(/<param5>(/<param6>(/<param7>(/<param8>(/<param9>(/<param10>(/<param11>(/<param12>(/<param13>(/<param14>(/<param15>)))))))))))))))')
->defaults(array(
'controller' => 'search',
'action' => 'index',
));
I would then test for them in the controller and parse them to a neat array. Is there any way to specify a route with any number of optional /key-value/ parameters?
EDIT
I noticed the request object has a nice parameter array already -- this leads me back to the Route::set question.. is there a way to phrase it allowing any number of parameters without the ugly ...(/<param14>(/<param15>))))))))... nesting?

No.
PS: someone can say again that short answers are bad, but there is nothing to say more: No, there is no such way.

Route::set('search', 'search(/<params>/)',array('params'=>'\.*'))
->defaults(array(
'controller' => 'search',
'action' => 'index',
));
tried?

Related

CakePHP routes always pointing to same action

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.

PHP Kohana 3.3 Controller Sub Folder

I've looked at so many Stackoverflow questions regarding this and none of them seem to solve my problem. I just want to have a admin folder and controllers within those. Here is what my route looks so far
/*
* Set the routes. Each route must have a minimum of a name, a URI and a set of
* defaults for the URI.
*/
Route::set( 'default', '(<controller>(/<action>(/<id>)))' )
->defaults( array(
'controller' => 'dashboard',
'action' => 'index',
) );
Route::set('admin','admin(/<controller>(/<action>(/<id>)))')
->defaults(array(
'directory' => 'admin',
'controller' => 'dashboard',
'action' => 'index',
));
As Kingkero said in his comment, move the route above the default one and it will work. if you read the docs on routing properly (I know it takes a while and a few reads for it all to sink in if you're new to it all, I've been there myself) it should be clear that the default route is a catch-all, and that any specific routes you need should come first, and any catch-all type routes after, as they are tried in sequence and when a match is found no more routes are tried.

Properly setting Cake routes with parameters

I'm making my first page which requires a parameter called $hash. My route is currently this:
Router::connect('/activate/*', array('controller' => 'users', 'action' => 'activate'));
which works almost perfectly, but if I enter a URL like:
http://localhost/activate/something/somethingelse/long/url
it matches the route. I want it to only match one 32 character long parameter like this:
http://localhost/activate/ajsheeugnbloiuffheogysswhvnntpdd
and for everything else to lead to a 404. What's the correct method of doing this?
You can set the regex matching for the route something like
Router::connect(
'/activate/:id',
array('controller' => 'users', 'action' => 'activate'),
array('id' => '[a-zA-Z]{32}')
);
you can adjust the regex, currently it's just letters.
Here's the reference: http://book.cakephp.org/2.0/en/development/routing.html#route-elements

Zend Framework 2: Catch all route and url escape

I'm trying to create or find a route which will basically catch all.
What I need is something that can route the something like the following;
/some-page/some-childpage/another-childpage
/another-page
/yet-anotherpage/page
These urls will not be related to any module as such, they're more so an admin can create pages at any url.
I've got something which catches the routes at the moment using wildcard routing and a child wildcard route, but when I use it in the URL view helper it's encoding the forward slashes within the 'url' parameter.
Basically:
$this->url( 'public_page', array( 'url' => 'foo/bar' ) )
Is outputting /foo%2Fbar.
As well as not allowing /s, when trying to retrieve the url parameter, its returning the query string upto the first /.
Any help and suggestions would be great!
Regards,
Michael
I'm not sure you could do that with an arbitrary number of segments. You could specify a segment route with the maximum amount of segments you expect and then just allow them all to be optional, but that seems a dirty way to do it if you're trying to use lots of segments.
'route' => '/:controller[[[[/:action]/:third]/:fourth]/:fifth]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]+',
'action' => '[a-zA-Z][a-zA-Z0-9_-]+',
),
'defaults' => array(
'controller' => 'Application\Controller\IndexController',
'action' => 'index',
'third' => 'something',
'fourth' => 'something-else',
),
You could possibly get something done with the Regex route, but again that's probably nasty as well.
http://framework.zend.com/manual/2.0/en/modules/zend.mvc.routing.html#zend-mvc-router-http-regex
I would probably suggest to use a better url structure as urls that deep aren't great anyway.

How to add route like example.com/module/controller/function/params in kohana

for example:
/modules/test/controllers/index.php - folder structure
example.com/test/index/param1/someValue - url
or something like this.
What I need to write in the bootstrap to make this path?
Route::set('test', 'test')
->defaults(array(
'directory' => 'modules/test/controllers',
'controller' => 'index',
'action' => 'index',
));
But this does not work.
I found the Kohana routing doc's a little lacking and wrote a very detailed overview that should answer your question: http://www.kineticklink.com/kohana-3-routing/
Note: I never got to the more advanced regular expression matching stuff--was going to be a part 3

Categories