php - Laravel 5 - Routes and variable parameters in controllers -


i want generate seo friendly urls when searching:
http://www.example.com/search (no filters)
http://**www.example.com/search/region-filter
http://**www.example.com/search/region-filter/city-filter

and paginate them in way:
http://www.example.com/search/2 (no filters, page 2)
http://**www.example.com/search/region-filter/2
http://**www.example.com/search/region-filter/city-filter/2

(sorry can't post more 2 links because of reputation)

so the second segment can filter or number of page (and same third one).

my laravel 5 routing file:

route::pattern('page', '[0-9]+'); ... route::get('search/{region}/{city}/{page?}', 'searchcontroller@index'); route::get('search/{region}/{page?}', 'searchcontroller@index'); route::get('search/{page?}', 'searchcontroller@index'); 

routes work fine because of 'page' pattern, inside controller petition http://**www.example.com/search/2 maps {page} in $region (even using last routing rule):

public function index($region='', $city='', $page='') 

codeigniter parameters mapped name, looks laravel maps them position, first 1 in $region.

is possible route parameters name instead of position or use laravel alternative them in controller? (i can count segments, ugly solution me)

you can use route::current() method access current route , parameters name via parameter method. there problem route definitions, make last 2 routes defined useless.

because page parameter in last 2 routes optional, depending on route path second , third routes not match properly, because of ambiguous definition of routes. below have test case proves point.


if have in controller:

public function index() {     $route = \route::current();      $region = $route->parameter('region');     $city = $route->parameter('city');     $page = $route->parameter('page');      $params = [         'region' => $region,         'city' => $city,         'page' => $page     ];      return $params; } 

you'll following results each route:

1. example.com/search/myregion/mycity/mypage:

{     "region": "myregion",     "city": "mycity",     "page": "mypage" } 

2. example.com/search/myregion/mypage:

{     "region": "myregion",     "city": "mypage",     "page": null } 

3. example.com/search/mypage:

{     "region": "mypage",     "city": null,     "page": null } 

so problem here not parameter matching order or name, it's route definitions. fix can have pagination in query string , drop altogether route definitions, because there's absolutely nothing wrong having pagination query string parameter if it's optional anyway. url this:

example.com/search/myregion/mycity?page=2 

you can check illuminate\routing\route class api see other methods have available there.


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -