Solve the problem of jumping to the home page when laravel uses authentication


When we use methods such as validate to verify, if it is an error, we will return to the home page

1. Directly in the request header

In the request header, accept uses appclipping / JSON, so that the accepted return of HTTP requests can only be in JSON format.

2. Modify frame function

//Remove $this - > getredirecturl() from the method
//It is not recommended. Generally, try not to modify the code of the framework

3. Recommended use

Define a baserequest base class

namespace App\Http\Requests;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;
class BaseRequests extends FormRequest
     *Validate failed to validate template
     * @param Validator $validator
    protected function failedValidation(Validator $validator)
        $message = '';
        foreach (json_decode(json_encode($validator->errors()),1) as $error){
            $message = $error[0];
        throw (new HttpResponseException(response()->json([
            'code' => 400,
            'msg'  => $message,
            'data' => []

Personally, I think this method is inexhaustible and not very good.

4. The solution I finally used

In a JSON request, the accept of the header can be specified as application / JSON. When accessing JSON, the error message will be returned. It is normal. If it is not JSON, the home page will be returned. To a certain extent, it is also a protection.

This is the end of this article about the solution of jumping to the home page when using laravel authentication. For more information about jumping to the home page when using laravel authentication, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!