我在request
中创建了Laravel 5.3
课程。看起来像这样:
<?php
namespace App\Http\Requests\User;
use Illuminate\Foundation\Http\FormRequest;
class StoreRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required|max:25|min:2'
];
}
}
但是当我在控制器中使用它时,我会收到:
NotFoundHttpException in RouteCollection.php line 161:
当我删除规则时,它看起来像这样:
public function rules()
{
return [
];
}
它突然起作用了!?!?
- UPDATE -
看起来验证器在web.php
中查找,但我没有路由(这就是我收到异常的原因:NotFoundHttpException in RouteCollection.php line 161:
)。我5.2
没有遇到这个问题。我该怎么办?
- UPDATE -
我目前的路线:
| Domain | Method | URI | Name | Action | Middleware |
+--------+-----------+-----------------------------------------------+--------------+---------------------------------------------------+------------+
| | POST | api/login | | App\Http\Controllers\Auth\AuthController@login | api |
| | GET|HEAD | api/register/{user}/verify/{ConformationCode} | | App\Http\Controllers\Auth\AdminController@confirm | api |
| | GET|HEAD | api/user | user.index | App\Http\Controllers\User\UserController@index | api,active |
| | POST | api/user | user.store | App\Http\Controllers\User\UserController@store | api,active |
| | GET|HEAD | api/user/create | user.create | App\Http\Controllers\User\UserController@create | api,active |
| | GET|HEAD | api/user/{user} | user.show | App\Http\Controllers\User\UserController@show | api,active |
| | PUT|PATCH | api/user/{user} | user.update | App\Http\Controllers\User\UserController@update | api,active |
| | DELETE | api/user/{user} | user.destroy | App\Http\Controllers\User\UserController@destroy | api,active |
| | GET|HEAD | api/user/{user}/edit | user.edit | App\Http\Controllers\User\UserController@edit | api,active
如果尝试使用新的Laravel 5.3
安装,我会收到错误!
也许与某些事情有关:
{p}Laravel 5.3
UserStoreRequest
中的
extends FormRequest
(use Illuminate\Foundation\Http\FormRequest;)
在Laravel 5.2
UserStoreRequest
extends Request
(use App\Http\Requests\Request;)
- 编辑 -
API.PHP
<?php
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::post('/login', 'Auth\AuthController@login');
Route::get('/register/{user}/verify/{ConformationCode}', 'Auth\AdminController@confirm');
Route::put('/test', 'User\UserController@test');
Route::group(['middleware' => ['active']], function () {
Route::resource('user', 'User\UserController');
Route::resource('corporation', 'Corporation\CorporationController');
Route::resource('forum', 'Forum\ForumController');
Route::resource('topic', 'Topic\TopicController');
});
商店方法
public function store(StoreRequest $request)
{
if($this->authorize($this->user))
{
$user = $this->userInfo->store($request);
event(new UserRegistered($user));
return response()->json(['success' => 'gebruiker succesvol aangemaakt. Welkom email verzonden.']);
}
return response()->json(['error' => 'niet geautoriseerd'], 401);
}
答案 0 :(得分:3)
这是因为FormRequest::response
函数想要将您重定向到包含错误的页面。但是因为你没有任何网络路线(只有api路线),它将失败并带有NotFoundHttpException
。如果您有任何Web路由,它将(内部)重定向到根目录。
如果您查看FormRequest::response
函数,可以看到这一点:
if($this->expectsJson()) {
return new JsonResponse($errors, 422);
}
因此,如果您可以在expectsJson
中填写这些预算,那么它会按预期运行。
您可以通过以下几种方式完成此操作:
X-Requested-With
标题确保请求是XMLHttpRequest。Accept
application/json
标头
wantsJson
调用以始终返回true。像这样: class JsonRequest extends Request
{
public function wantsJson() {
return true;
}
}