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.

10
  • Please show the form or a link you use to execute the register() method. Commented Jan 7, 2018 at 9:46
  • Updated @AlexeyMezenin Commented Jan 7, 2018 at 9:52
  • It is related to wrong routes Commented Jan 7, 2018 at 9:54
  • I suspect there's no problem with route it self, when i change using standard Request it works properly. @user2486 Commented Jan 7, 2018 at 9:56
  • show us your exact error Commented Jan 7, 2018 at 9:59

3 Answers 3

21

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@Leo is it available on all version of laravel??
I can't believe I lost an hour of my life to this — especially when I wrote docs for my own API mandating the Accept header. Thank you for this answer.
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.

1 Comment

I still do not understand, which part do a wrong logic? And what should i do? Give me a clue.
-1

Disable Accept: */* in headers.

1 Comment

By what mechanism does this help?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.