我正在尝试验证API中的请求,但是当我使用validate方法或什至创建扩展FormRequest的类时,它给我405错误。在下面,您可以看到我的控制器和定义路由的方式。当验证失败时,它只会给我405错误,我认为,也许是某种程度上有一个回调函数发送GET方法请求,但我不知道如何解决它。
<?php
namespace App\Http\Controllers\API;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Interfaces\PostRepositoryInterface;
class PostController extends Controller
{
private $postRepository;
public function __construct(PostRepositoryInterface $postRepository)
{
$this->postRepository = $postRepository;
}
public function index()
{
return $this->postRepository->all();
}
public function store()
{
request()->validate([
'title'=>'required|min:2',
]);
return $this->postRepository->createPost();
}
public function show($postId)
{
return $this->postRepository->findById($postId);
}
}
错误消息:
Route::group(['middleware' => 'auth:api', 'namespace'=> 'API'], function(){
Route::get('post' , 'PostController@index' );
Route::post('post' , 'PostController@store' );
Route::get('post/{post}' , 'PostController@show' );
Route::get('post/{post}/edit', 'PostController@edit' );
Route::post('post/{post}' , 'PostController@update' );
Route::post('post/{post}' , 'PostController@destroy');
});
答案 0 :(得分:0)
感谢所有尝试提供帮助的人,我在这篇文章中找到了解决方法
MethodNotAllowedHttpException using Form Request Validation on Laravel 5.5