Hello everyone, I ran into a small problem. Suppose there are routes:

Route::get('category/{id}/{action}') 

It turns out that links in the form

 /category/1/show /category/1/edit /category/1/remove 

Approach our route. All this is good, but it is necessary that they be carried out in different methods . And this is no longer possible. I tried this way:

 Route::get('category/{id}/{action}', "Controller@show")->where("action"=>"show") Route::get('category/{id}/{action}', "Controller@edit")->where("action"=>"edit") Route::get('category/{id}/{action}', "Controller@remove")->where("action"=>"remove") 

But with such a scheme, the last route is always executed.

I’m not suggesting an option with a pars URL. I want to understand whether it is possible to do this by means of laravel

  • one
    Is the standard Route::resource('category', 'Controller') not suitable for this purpose? - Rustam Gimranov
  • Also, you need to write so that they are executed in different controllers , but in the example code you have one controller and different methods. - Rustam Gimranov
  • @RustamGimranov sorry, you are right, you must be executed in different methods. - Evgeny Nikolaev
  • Then I do not see the catch in the question) But if you know that "action"=>"show" , then why not Route :: get ('category / {id} / show', "Controller @ show")? In general, Route::resource('category', 'Controller') is more correct - Rustam Gimranov
  • @RustamGimranov I need this {action} use as an intermediary so as not to write the same code in the controller maths - Evgeny Nikolaev

2 answers 2

I advise first of all to get acquainted with the documentation. In order not to produce such actions, it is enough to create a resource controller and specify the same type - resourse in the route.

More details can be found here: https://laravel.com/docs/5.8/controllers#resource-controllers

    You can register different methods in one controller and, depending on the action, execute these methods. For example, in web.php, leave only one line.

     Route::get('category/{id}/{action}', "Controller@router"); 

    In the controller, do so

     public function router($id, $action) { if ($action == 'show') { $this->show($id); } } public function show($id) { //ваш код } public function edit($id) { //ваш код } public function remove($id) { //ваш код }