Laravel routing url with variable order of parameters -
i looking @ routing controller url parameters can vary in number or order in appear in url. there many such combinations , want invoke same controller action of these urls
examples of how urls like:
- route::get('route1/id/{id}', 'controller1@controlleraction1');
- route::get('route1/id/{id}/name/{name}', 'controller1@controlleraction1');
- route::get('route1/name/{name}', 'controller1@controlleraction1');
- route::get('route1/id/{id}/name/{name}/orderby/{orderby}', 'controller1@controlleraction1');
- route::get('route1/id/{id}/orderby/{orderby}', 'controller1@controlleraction1');
also in controller action, want break query string array. second example mentioned above, want query string id/{id}/name/{name}
converted array ('id' => {id}, 'name' => {name})
to invoke same controller action different variations of urls, have following code in routes.php:
route::get('route1{all}', 'controller1@controlleraction1')->where('all', '.*')
which seems invoke "controlleraction1
" of controller1
different types of urls mentioned above.
and in function controlleraction1
, doing
$route_input = route::input('all'); var_dump($route_input); prints "/id/1/name/xyz" when hit http://example.com/laravel/public/route1/id/1/name/xyz
i know if:
- doing route::get('route1{all}', 'controller1@controlleraction1')->where('all', '.*') right method invoke same action variable combination of parameters?
- does laravel offer function convert "/id/1/name/xyz" array('id' => 1, 'name' => 'xyz') or need write custom function?
- is there better way achieve requirements?
i believe not. plus, in way won't able understand values being passed.
even if there one, think don't need pass array. imho, prefer keep items separate, manipulate them controller. personal suggestion, if need array of data, why don't use post method? (the right answer, want users able save link :p )
the complicated part request, want keep under same controller action, messes routes. try (in
routes.php
):
route::pattern('id', '[0-9]+'); route::pattern('name', '[a-z]+'); route::get('route1/{id}/{name?}/{orderby?}', 'controller1@controlleraction1'); route::get('route1/{name}/{orderby?}', 'controller1@controlleraction1');
in way:
you can have route id, name , orderby optional
if no id passed, can have route name, orderby optional
note how different urls: it's more complicated put routes wrote them id/{id}/name/{name}
, in way proposed {id}/{name}
. if need them way, why don't call links passing variables function follows? http://www.yoursite.com/route1?id=xxxx&name=yyyy&orderby=zzzz
Comments
Post a Comment