0

我是restful api的新手,我遇到了一个问题,问题是:当我用delete方法请求destroy并用post方法存储时,两者都会返回500错误。但我用get方法请求索引和显示,都可以。问题是什么?这是我的代码和要求:

删除 http://***.com/RestfulPrac/public/customers/10000001

获取 http://***.com/RestfulPrac/public/customers/10000001

发布 http://***.com/RestfulPrac/public/customers


 class CustomersController extends Controller
 {
    public function index(){

    $customersInfo = customers::all();
    return $customersInfo;

    }

    public function show($cust_id){

    $customer = customers::where('cust_id',$cust_id)->first();
    return $customer;
    }
    public function store()
    {

    echo "store";
    }

   public function destroy()
   {

      return "success";
   }
}

Route::resource('customers','CustomersController');

apache access.log : "DELETE /RestfulPrac/public/customers/1000000001 HTTP/1.0" 500 20246 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102野生动物园/537.36"

apache error.log:[Thu Jun 02 09:09:24.324782 2016] [negotiation:error] [pid 4328:tid 1676] [client 127.0.0.1:4940] AH00690:没有可接受的变体:D:/XAMPP/apache/错误/HTTP_NOT_FOUND.html.var

laravel.log: local.ERROR: exception 'Illuminate\Session\TokenMismatchException' in F:\PhpstormProjects\RestfulPrac\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken.php:67 堆栈跟踪:

如果有人可以帮助我,我将不胜感激!在此先感谢!

4

2 回答 2

1

根据 Laravel 错误日志,您的 csrf 令牌不匹配。如果您正在构建 API,您可能不想使用“网络”中间件。该中间件组正在启动一个会话,并将检查所有未使用 READ(GET、HEAD、OPTIONS)HTTP 方法的请求上的 csrf 令牌。

默认情况下,当 Laravel 将所有路由routes.php加载到您的RouteServiceProviderin app\Providers.

根据 Laravel 错误日志,这可能是从哪里开始。

这可能会有所帮助:VerifyCsrfToken always called when route to API Middleware Laravel 5.2.35

于 2016-06-02T02:00:29.857 回答
0

只需导航到app\kernel.php

像这样评论 csrf

protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        //  \App\Http\Middleware\VerifyCsrfToken::class,
    ],

    'api' => [
        'throttle:60,1',
    ],
];

如果您正在构建 api,则不需要 csrf 保护

于 2016-06-02T02:05:43.543 回答