Laravel路由的一些不清楚的行为

时间:2018-02-17 10:51:16

标签: json laravel post routes multipartform-data

我目前的情况有点困惑。 我有这样的路线:

Route::get('clients', 'ClientController@index');
Route::post('clients', 'ClientController@store');

控制器中的index()和store()方法。 这是RESTful API的一部分,所以我想接受json的请求。

所以这是我的POST JSON请求:

> POST /api/clients/ HTTP/1.1
> Host: dfs.test
> User-Agent: insomnia/5.14.6
> Cookie: XSRF-TOKEN=eyJpdiI6IjYrejBPZE5CbnRXcmg3cEZnOWorbGc9PSIsInZhbHVlIjoieVNjK1NlVGt3R0lmY3hkWG1ybXBuQWxBdlBRSnBiUlNZOGp1WU5rVWw2RFhWVXlNWExIZGNSUUQ0ZXBuMUZxb3g1RVIxWEhITWdPd2hiaVlCc3NKZUE9PSIsIm1hYyI6ImE1OWUxMWNiNzJiMDRkYTVmYjUwYzUwNjZhODM5NWVkNmJjYWZkODE2ZDk0ODI2NzY5MmQyNmRlNmYzYmQxNWEifQ%3D%3D; dfs_session=dnGZsrMSwbkA7szMR5oAJsTIlAkJW98w1htDFJjW; laravel_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjEsImNzcmYiOiI3YUZpVk9BS0pJNkVYZzJJS1pWSU85WWxYVWtaZ3FjSFVnZEJ0eGI5IiwiZXhwaXJ5IjoxNTE4ODY5OTQzfQ.aQp6Nm37QyTIJgIUuHd0T_8-2Sap5WqcS8WM6K5G3Zs
> Content-Type: application/json
> Accept: application/json
> Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjJhZjU5NmNlY2VmOGE5ZWNiZjgwZWM3NWRmNTlkOTAyMzZhZDRhM2I5ODA2YjAxMDMzMmVmZDFmMDA4YzcyNmNjMTFkZmUxM2Q2Y2ZlM2E5In0.eyJhdWQiOiI0IiwianRpIjoiMmFmNTk2Y2VjZWY4YTllY2JmODBlYzc1ZGY1OWQ5MDIzNmFkNGEzYjk4MDZiMDEwMzMyZWZkMWYwMDhjNzI2Y2MxMWRmZTEzZDZjZmUzYTkiLCJpYXQiOjE1MTg3OTQ2NzQsIm5iZiI6MTUxODc5NDY3NCwiZXhwIjoxNTUwMzMwNjc0LCJzdWIiOiIxIiwic2NvcGVzIjpbImFwaS1tYWluLXJlYWQiLCJhcGktbWFpbi1kZWxldGUiLCJhcGktbWFpbi11cGRhdGUiLCJhcGktbWFpbi1jcmVhdGUiXX0.zpe2rYfZxAB6xiKasQbEvt2D_4TbGp9xjSeLWNklhd2YUwDIgyOubQUMz4dAOcfjbuxQnG5h3Mc2LaidIdvMiXt53nuE9I8_9Kq9Sx6GCZ4vRpWiXAATe5d7oDDXxp6147Rtkd9O0io0Wcc0yhtgRRvSwxAzYbOnnENR3cICsNymEVhNJr5yrH2xIxhfsJrgWaFuPCpslCava53AuwO7Urm2CXPImXIP5dVqqn8qOGA_-LkJTE3S9yagmU5-3niubLqlpoGA55yv-fcYZo90sFX68VM4PSIh1xGjX_3K9Tot_BFfCAOFDoYujv51dpf94uecFHnzUyulcLXYstGImGJTWnfRIiqA7pExo3bkZ8F1PUXOiYW8N9yzwe_6_2wpzUEJqemk7mEesF7mzk5rYldix3ZWh2wxu_crgD6faRiNpbsG3m0zSPNfLMny0ckYTnPyibrsVPklndJWHaoe1yrroD_jFoRe31LQh3iQFhIxNOVIACAwkvXK1Y5w98KsqbE8lyS2JWtA-xiHYjVmLsJsp6G_N9OFVDl-iX4Hn57H_-AtbsV6iy6Vi9I40v4zx9r7U3GkFRedYhu8rUxa2bTtScwfxOzJCav3R__sS1cJiMwa8EYliPAEenkCN5e8iP-4tWm8Qpmn0kV5vgQzABKsZVulY5KWdkHFDu0G1-M
> Content-Length: 17
| {
|   "asd": "asd"
| }

如果我使用“Content-type:application / json”或“application / x-form-www-urlencoded”从一些REST客户端(例如Insomnia或Postman)发送上述请求,Laravel将把我发送到index()方法。但我为GET请求定义了它!怎么回事?

此外,如果我使用“multipart / form-data”标题发送一些帖子数据,Laravel将路由到controllers store()方法。

因此,Laravel仅使用multipart / form-data标头路由POST请求? 如果是的话,当Laravel路由到错误的方法时,如何接受json体?

我的Laravel版本是5.6。

1 个答案:

答案 0 :(得分:0)

好的,经过几天的谷歌搜索后,这里就是解决方案:Laravel 5 POST routes to index instead of store