我无法检查简单的 post 路由,出现 405 Method Not Allowed 错误
I can't check simple post route, i get 405 Method Not Allowed error
只是我有以下路线:
Route::post ('clientLoginAccount', 'AuthController@clientLoginAccount');
我想 post 一些数据作为 json 对象或其他格式,我尝试通过 RESTClient
Firefox 上的插件测试这条路线,但我得到错误:
405 Method Not Allowed
clientLoginAccount
AuthController
方法
public function clientLoginAccount()
{
echo json_encode(['test'=>' ok']);
}
路线列表:
| | POST|clientLoginAccount | | App\Http\Controllers\AuthController@clientLoginAccount | web |
排除该路线的 CSRF-Token Verficiation
。
打开VerifyCsrfToken中间件并添加clientLoginAccount
到$except
数组
class VerifyCsrfToken extends BaseVerifier
{
protected $except =
'clientLoginAccount'
];
}
当您或其他人使用您的 API
时,您必须预料到 405 Method Not Allowed
是一个非常大的问题,您应该在控制器内部管理它,您的问题是表单方法而不是 CSRF
。在任何情况下,您都应该使用此解决方案,例如:
class AuthController extends Controller
{
public function clientLoginAccount(Request $request)
{
if ($request->method() != 'POST') {
return response()->json(['data' => "Request must be 'POST'", 'status' => '1'], 200);
}
}
}
只是我有以下路线:
Route::post ('clientLoginAccount', 'AuthController@clientLoginAccount');
我想 post 一些数据作为 json 对象或其他格式,我尝试通过 RESTClient
Firefox 上的插件测试这条路线,但我得到错误:
405 Method Not Allowed
clientLoginAccount
AuthController
public function clientLoginAccount()
{
echo json_encode(['test'=>' ok']);
}
路线列表:
| | POST|clientLoginAccount | | App\Http\Controllers\AuthController@clientLoginAccount | web |
排除该路线的 CSRF-Token Verficiation
。
打开VerifyCsrfToken中间件并添加clientLoginAccount
到$except
数组
class VerifyCsrfToken extends BaseVerifier
{
protected $except =
'clientLoginAccount'
];
}
当您或其他人使用您的 API
时,您必须预料到 405 Method Not Allowed
是一个非常大的问题,您应该在控制器内部管理它,您的问题是表单方法而不是 CSRF
。在任何情况下,您都应该使用此解决方案,例如:
class AuthController extends Controller
{
public function clientLoginAccount(Request $request)
{
if ($request->method() != 'POST') {
return response()->json(['data' => "Request must be 'POST'", 'status' => '1'], 200);
}
}
}