php - How laravel handles same get routes -


i building custom routing system home grown framework. question how laravel handles same routes in routing system. example

route::get('api/users/{user}', function (app\user $user) {     return $user->email; });  route::get('api/user/{pass}', function (app\user $user) {     return $user->email; }); 

the number of arguments in route api/user/{pass} & api/users/{user} same. how ?. how differentiate routes ?. how matching process works ?.

laravel looks routes secuentially, means given 2 routes same endpoint, call first 1 found , stop there, second 1 never reached, example:

// url user/johndoe  // start looking match  route::get('user/{name}', function ($name) { // route match      // callback called , laravel stops searching     return $name; });  route::get('user/{id}', function ($id) { // route match     // callback been called 1 never reached     return $id; }); 

in case want differentiate both routes can using regex:

route::get('user/{name}', function ($name) {     // callback executed when word passed in })->where('name', '[a-za-z]+');  route::get('user/{id}', function ($id) {     // callback executed when number passed in })->where('id', '[0-9]+'); 

as can see, both of routes have same endpoint, filter parameter given according regex provide. hope helps you.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -