简体   繁体   中英

Laravel 5.1 - How to access dynamic URI segment in Controller and Route

Team,

I am using Laravel 5.1, In which I have to use URI segment feature same as Codeigniter

Like eg. URL - www.example.com/user_id/user_type/user_role/....

Want to access those parameters user_id , user_type , user_role in controller and also want to manage the Route file.

Can anyone guide me how to do this in LARAVEL?

Thanks in Advance

In your routes.php file:

Route::get('user/{user_id}/{user_type}/{user_role}', ['uses' => 'UserController@index', 'as' => 'user.index']);

In your UserController.php file:

public function index($user_id, $user_type, $user_role) {
  dd($user_id, $user_type, $user_role);
}

In your route.php file write this - If you have any optional parameter then put a Question mark(?) after that, I assumed that the parameter user_role is optional.

 Route::get('user/{user_id}/{user_type}/{user_role?}', [
    'uses' => 'UserController@getIndex',
    'as' => 'user.get.index' // You can write any unique name you want, This will be your route name.
 ]);

In your Controller, You can access these parameters like this -

 public function getIndex($user_id, $user_type, $user_role) {
    // Here your parameters will be available to use.
   //  Write your logic
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM