当前位置:网站首页>There is no response to redirection and jump in the laravel constructor [original]

There is no response to redirection and jump in the laravel constructor [original]

2022-06-26 04:20:00 Telkobe

Link to the original text

First contact laravel There will be such a problem , When we judge that the user is not logged in and jump to the login page, we usually create a controller base class , Judge in the constructor of the base class , Redirect to the login page if you are not logged in

I use tp More , stay tp The constructor of the controller can be used directly redirect Function to perform a jump action , However, in laravel But not , There is one thing to mention here , That's middleware , It can perfectly meet our needs , Of course, this article is only for novice reference , Bosses do not spray .

First, in the app\Middleware Create a new middleware under the directory , Here I'll name it CheckLogin

namespace App\Http\Middleware;
use Closure;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
class CheckLogin extends Middleware
{
    public function handle($request, Closure $next, ...$guards)
    {
        $response = $next($request);
        if(!session('user')){// Judge whether the user has not logged in and jump to the login page 
            return redirect('login');
        }
        // If you are logged in, execute the normal request 
        return $response;
    }
}

And then in app/Http/Kernel.php Inside $routeMiddleware In an array , Add the newly created middleware , Alias name yourself . My name here is 'check.login'.

image.png

Create a new controller base class , Register the middleware in the constructor of the base class . In addition to the login controller, other login controllers need to inherit this base class , Here I'll name it BaseController.

namespace App\Http\Controllers\Index;
use App\Http\Controllers\Controller;
class BaseController extends Controller{
    public function __construct()
    {
       $this->middleware('check.login');// Detect login 
    }
}

In this way, the request will be redirected to the login page if the user is not logged in .

tp There is also middleware inside , The usage is basically the same , Because I'm lazy and can redirect the jump directly, it doesn't work very well .

PS:2019/10/24 Today I am studying tp6 Found that , direct redirect('xxx/xxx')->send() that will do , instead of return, Due to the framework request process mechanism , Pay attention to this way of writing if you send When the following code is closely connected with the conditions to determine whether to log in, it is used in send Errors may be reported later , Because the framework is responding to the send The entire request process does not end after the method is output , So if you encounter an error, you can report it in send And then forcibly terminate the code , Such as :redirect('xxx/xxx')->send();exit();, However, it is best to eliminate these errors by modifying the judgment .

原网站

版权声明
本文为[Telkobe]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202180535384819.html