简体   繁体   English

方法发布,放置,删除路线在laravel 5上不起作用

[英]method post, put, delete route not work on laravel 5

I try test api rest on laravel 5 but I have problems with method post, put, delete. 我尝试在laravel 5上测试api,但是方法发布,放置,删除时遇到问题。
In my route.php file I have code: 在我的route.php文件中,我有以下代码:

Route::group(['prefix' => 'api'], function()
{
    Route::group(['prefix' => 'user'], function()
    {
        Route::get('', ['uses' => 'UserController@allUsers']);

        Route::get('{id}', ['uses' => 'UserController@getUser']);

        Route::post('', ['uses' => 'UserController@saveUser']);

        Route::put('{id}', ['uses' => 'UsercCntroller@updateUser']);

        Route::delete('{id}', ['uses' => 'UserController@deleteUsers']);

    });
});

Route::get('/', function()
{
    return 'Enjoy the test...';
});

and in UserController.php have code: 并且在UserController.php中具有代码:

public function allUsers()
{
   return 'test';
}

public function getUser($id)
{
    return 'test get user';
}

public function saveUser()
{
    return 'test save user';
}

public function updateUser($id)
{
    return 'test update user';
}

public function deleteUsers($id)
{
    return 'test delete user';
}

When I run with method get it works good but with method post, put and delete it does not work. 当我使用get方法运行它时,但在方法post中运行时,放置和删除它不起作用。
Why is this? 为什么是这样?

If you want to make REST APIs then use laravel's generators. 如果要制作REST API,请使用laravel的生成器。

Use php artisan make:controller UserController 使用php artisan make:controller UserController

Laravel automatically creates RESTful controller class for you with all required methods. Laravel使用所有必需的方法自动为您创建RESTful控制器类。

Then just put one line in your routes.php 然后只在您的routes.php放一行

Route::group(['prefix' => 'api'], function()
{
    Route:resource('user', 'UserController');
});

And that's it, now you can access get, post, put, and delete requests very easily. 就是这样,现在您可以非常轻松地访问获取,发布,放置和删除请求。

If you want to see what route I should use for what method then simply fire php artisan route:list from commandline. 如果您想查看应该使用哪种方法的php artisan route:list则只需php artisan route:list启动php artisan route:list

And because of laravel comes with built in csrf token verification middleware, you must have to pass _token with your post data request. 而且由于laravel内置了csrf令牌验证中间件,因此您必须在发布数据请求中传递_token Or either you can access those routes without csrf token verification by doing this: 或者,您可以通过执行以下操作来访问这些路由,而无需进行csrf令牌验证:

Go to kernel.php in Http folder under the app directory, and comment the csrfToken line. 转到kernel.php中的Http文件夹中的应用程序目录下,并发表意见csrfToken线。

protected $middleware = [
    'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
    'Illuminate\Cookie\Middleware\EncryptCookies',
    'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
    'Illuminate\Session\Middleware\StartSession',
    'Illuminate\View\Middleware\ShareErrorsFromSession',
 // 'App\Http\Middleware\VerifyCsrfToken',
];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Laravel:Route::resource() GET & POST 工作,但 PUT & DELETE 抛出 MethodNotAllowedHttpException - Laravel: Route::resource() GET & POST work, but PUT & DELETE throw MethodNotAllowedHttpException 此路由不支持 POST 方法。 支持的方法:PUT、PATCH、DELETE - The POST method is not supported for this route. Supported methods: PUT, PATCH, DELETE 此路由不支持 PUT 方法。 支持的方法:POST。 在 laravel 8 - The PUT method is not supported for this route. Supported methods: POST. in laravel 8 为什么 laravel ApiResource Route PUT 方法不起作用? - Why laravel ApiResource Route PUT method doesn't work? Laravel 8 - 此路由不支持 POST 方法。 支持的方法:删除 - Laravel 8 - The POST method is not supported for this route. Supported methods: DELETE Laravel 中的表单请求验证:需要取决于方法(POST、PUT 或 DELETE) - Form request validation in Laravel: required depending on the method (POST, PUT or DELETE) 此路由不支持 POST 方法 laravel 7 - The POST method is not supported for this route laravel 7 Laravel:此路由不支持 POST 方法 - Laravel: The POST method is not supported for this route 消息:此路由不支持 POST 方法。 支持的方法:GET、HEAD、PUT、PATCH、DELETE - message: The POST method is not supported for this route. Supported methods: GET, HEAD, PUT, PATCH, DELETE 此路由不支持 POST 方法。 支持的方法:GET、HEAD、PUT、PATCH、DELETE。 在添加字段时 - The POST method is not supported for this route. Supported methods: GET, HEAD, PUT, PATCH, DELETE. while adding a field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM