1

This is pretty strange when i use Form Request Validation on Laravel 5.5, all my post request gonna be 405 Method Not Allowed, but getting normal when i use standard validation, here my code is:

php artisan route:list value

+--------+----------+----------------------------------------+--------------------+-----------------------------------------------------------+------------+
| Domain | Method   | URI                                    | Name               | Action                                                    | Middleware |
+--------+----------+----------------------------------------+--------------------+-----------------------------------------------------------+------------+
|        | POST     | api/register                           |                    | App\Http\Controllers\AuthController@register              | api        |
+--------+----------+----------------------------------------+--------------------+-----------------------------------------------------------+------------+

Request using insomnia: enter image description here

My base_api value in insomnia is http://mylocal.app/api

Error message:

Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException

Error display messages: enter image description here

Router (api.php):

Route::post('register', 'AuthController@register');

Controller (AuthController.php):

use App\Http\Requests\StoreRegistration;

public function register(StoreRegistration $request)
{

    $email = $request->email;
    $name = $request->name;
    $password = $request->password;

    $user = User::create([
        'name' => $name,
        'email' => $email,
        'password'  => Hash::make($password)
    ]);

    $verifyUser = VerifyUser::create([
        'user_uuid' => $user->uuid,
        'token' => str_random(100)
    ]);

    SendVerificationEmail::dispatch($user); //I use queue to send email

    return response()->json(['success' => true, 'message' => 'message on success']);
}

StoreRegistration.php :

class StoreRegistration extends FormRequest {
public function authorize()
{
    return true;
}

public function rules()
{
    return [
        'name' => 'required|min:4|max:50|unique:users|alpha_dash',
        'email' => 'email|required|max:255|unique:users',
        'password' => 'required|confirmed|min:6'
    ];
}
}

This problem makes me crazy.

roman
  • 788
  • 1
  • 10
  • 23

3 Answers3

20

when you send the request from insomnia make sure to add the headers;

accept: application/json and Content-Type: application/json

that way laravel knows that its an api request and will use routes defined on api.php and not web.php routes.

Leo
  • 7,274
  • 5
  • 26
  • 48
2

The error didn't come from the validation.

It was because when you use FormRequest class, you are using its default failedValidation method which is for web and redirect to previous page with error. In your case, you are using api but it redirecting to a url that doesn't exists. When you are using standard validation, you are specifying your own logic.

throw new ValidationException($validator, $this->response(
        $this->formatErrors($validator)
    ));

So, you need to make your custom failed validation approach with your api. You can do something like in your StoreRegistration class, you can override failedValidation method to the following-

public $validator = null;
protected function failedValidation(\Illuminate\Contracts\Validation\Validator $validator)
{
    $this->validator = $validator;
}

Then your controller method, do anything you want with your $validator object. May be something like the following-

if (isset($request->validator) && $request->validator->fails()) {
    return response()->json($request->validator->messages(), 400);
}

Have a look at this link previously I answered.

Sohel0415
  • 9,523
  • 21
  • 30
  • I still do not understand, which part do a wrong logic? And what should i do? Give me a clue. – roman Jan 07 '18 at 10:22
-1

Disable Accept: */* in headers.

Ahmed5G
  • 310
  • 2
  • 8