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

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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?