Laravel 5.3抛出错误formrequest

时间:2016-09-28 15:51:53

标签: php request laravel-5.3

我在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);
    }

1 个答案:

答案 0 :(得分:3)

这是因为FormRequest::response函数想要将您重定向到包含错误的页面。但是因为你没有任何网络路线(只有api路线),它将失败并带有NotFoundHttpException。如果您有任何Web路由,它将(内部)重定向到根目录。

如果您查看FormRequest::response函数,可以看到这一点:

if($this->expectsJson()) {
    return new JsonResponse($errors, 422);
}

因此,如果您可以在expectsJson中填写这些预算,那么它会按预期运行。

您可以通过以下几种方式完成此操作:

  • 通过提供X-Requested-With标题确保请求是XMLHttpRequest。
  • 提供X-PJAX标题
  • 使用Accept
  • 提供application/json标头
  • 覆盖请求上的wantsJson调用以始终返回true。像这样:

class JsonRequest extends Request { public function wantsJson() { return true; } }