When dealing with the problem of permission judgment, thinkphp5.1 middleware how to realize page redirection and display prompts.

problem description

assuming that the user is required to log in before publishing new information, determine whether the user is logged in or not and redirect the page by registering the middleware in the controller layer.

the environmental background of the problems and what methods you have tried

I intend to redefine the page by modifying the url within the request object, but this does not convey a prompt or redirect the page.

related codes

public function handle($request, \Closure $next)
    {
        dump(session("userInfo"));
        if (session("?userInfo")) {
            return $next($request);
        } else {
            $request->url = "/tp5/public/?s=/user/signIn";
            return $next($request);
        }
    }
Php
Feb.10,2022

The assignment method in the

problem code only assigns values to param ['url']. You can use the redirect helper function directly.

public function handle($request, \Closure $next)
    {
        if (session('?userInfo')) {
            return $next($request);
        } else {
            return redirect('/tp5/public/?s=/user/signIn',['message'=>'']);
        }
    }
Menu