dpnfjx755573 2017-11-07 21:30
浏览 965
已采纳

Laravel Route资源既有GET又有POST

I have this Route

Route::group([ 'middleware' => ['auth','lang']], function() {

    // SETTINGS
    Route::namespace( 'Settings' )->prefix( 'settings' )->group( function () {

        // INDEX
        Route::get( '/', 'SettingsController@index' );

        // ACCOUNTS
        Route::resource( 'accounts', 'AccountController', ['only' => ['index','store','edit','update']] );

        // TAGS
        Route::resource( 'tags', 'TagController', ['only' => ['index','destroy']] );

        // PROFILE
        Route::get('profile', 'ProfileController@index');
        Route::post('profile', 'ProfileController@update');

    }); 

Any way I can join the two PROFILE ones into one that is resource? Whenever I try using Route::resource( 'profile', 'ProfileController', ['only' => ['index','update']] ), it gives me an error that the method is not allowed - 405 (Method Not Allowed). I think it just doesn't find the update one? I am really not sure what might be the issue.

  • 写回答

1条回答 默认 最新

  • douyalin0847 2017-11-07 21:36
    关注

    This is happening because in the case of resourceful controllers, a post would be defaulted to a store method, not update.

    So you are posting to the store method, which is not defined, giving you the 403 method not allowed.

    To solve this, either change your request to a PUT or change your code to Route::resource( 'profile', 'ProfileController', ['only' => ['index','store']] ) Keep in mind, if you do this, you have to move the contents of your update function to store.

    For more information, checkout https://laravel.com/docs/5.5/controllers#resource-controllers

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

报告相同问题?

悬赏问题

  • ¥15 求指导ADS低噪放设计
  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存