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 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛