【问题标题】:Laravel Route Resource both GET and POSTLaravel 路由资源 GET 和 POST
【发布时间】:2018-04-20 10:25:28
【问题描述】:

我有这个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');

    }); 

有什么方法可以将两个 PROFILE 合并为一个 resource?每当我尝试使用 Route::resource( 'profile', 'ProfileController', ['only' => ['index','update']] ) 时,它都会给我一个错误,即不允许使用该方法 - 405 (Method Not Allowed)。我认为它只是找不到update 吗?我真的不确定可能是什么问题。

【问题讨论】:

    标签: php laravel post get routes


    【解决方案1】:

    发生这种情况是因为在资源丰富的控制器的情况下,帖子将默认为存储方法,而不是更新。

    所以你发布到 store 方法,没有定义,给你 403 方法是不允许的。

    要解决此问题,请将您的请求更改为 PUT 或将您的代码更改为 Route::resource( 'profile', 'ProfileController', ['only' => ['index','store']] ) 请记住,如果您这样做,您必须将更新函数的内容移动到存储中。

    欲了解更多信息,请查看https://laravel.com/docs/5.5/controllers#resource-controllers

    【讨论】:

    • 完美。谢谢!
    猜你喜欢
    • 2016-08-16
    • 2012-02-09
    • 2015-05-28
    • 2013-11-08
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    • 2018-10-15
    相关资源
    最近更新 更多