dongshuohuan5291 2018-12-25 13:06
浏览 57
已采纳

laravel 5.7如何将一个控制器的变量id传递给其他控制器

I have two controllers and I try pass id of variable form method one controller to method to second controller and I got an error like this >MethodNotAllowedHttpException . I will add that my url after the action looks like this >http://localhost/comment?12 . How is the best way of solving this problem ?

  • 写回答

1条回答 默认 最新

  • dongyin6576 2018-12-25 14:03
    关注

    You are most probably getting MethodNotAllowedException, because you are opening a route that is defined as a POST route via GET or the other way around.

    To avoid that you can use php artisan route:list and get a list of all defined routes and see how you should "access" them:

    +--------+-----------+----------------------------------------------------+------------------------+------------------------------------------------------------------------+--------------+
    | Domain | Method    | URI                                                | Name                   | Action                                                                 | Middleware   |
    +--------+-----------+----------------------------------------------------+------------------------+------------------------------------------------------------------------+--------------+
    |        | GET|HEAD  | /a/show/{id}                                       |                        | App\Http\Controllers\AController@show                                  | web          |
    |        | GET|HEAD  | /b/show/{id}                                       |                        | App\Http\Controllers\BController@show                                  | web          |
    

    So let's say you have 2 controllers: AController and BController. Each of the controllers have a show() method declared in them.

    class AController extends Controller {
    
        //... other AController related code
    
        public function show($id) {
            dd($id);
        }
    }
    
    
    class BController extends Controller  {
    
        //... other BController related code
    
        public function show($id) {
            dd($id);
        }
    
    }
    

    Then you can define your routes like this:

    Route::get('/a/show/{id}', 'AController@show');
    

    Links like: example.com/a/show/10 will "load" AController's show() method. All we have in our AController::show() method's body is dump and die on $id, we will get 10 printed if we visit that link.

    We can replace that dd($id); with:

    redirect()->action('BController@show', ['id' => $id]);
    

    And define another route:

    Route::get('/b/show/{id}', 'BController@show');
    

    This way if we open the previous link: example.com/a/show/10, we will be redirected to: example.com/b/show/10 and BController::show() method will be executed and it prints the variable using dump and die.

    Key points:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办