当前位置:网站首页>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】
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'.

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 .
边栏推荐
- Zhubo Huangyu: you can try these non-agricultural operation skills
- Resolve PHP is not an internal or external command
- Verrouillage de lecture et d'écriture pour la synchronisation des fils
- Computer network high frequency interview questions
- NPM installation tutorial
- 2021 year end summary
- But the Internet began to have a new evolution and began to appear in a new state
- Upload script file (one sentence back door) WAF bypass (PHP)
- Development prospect and investment strategic planning report of global and Chinese PVC hose industry from 2022 to 2028
- C generic
猜你喜欢

Gateway can not connect to tcp://127.0.0.1: Connection refused

Nailing open platform - applet development practice (nailing applet server side)

Ubuntu installs PostgreSQL and uses omnidb to view

Using jsup to extract images from interfaces

OSS CDN alicloud configuration method

Matplotlib multi line chart, dot scatter chart

1. foundation closing

线程同步之互斥量(互斥锁)
![[Qunhui] no port access (reverse proxy + intranet penetration)](/img/bc/b1e0c5c382e30fbcea28fbc68c1151.jpg)
[Qunhui] no port access (reverse proxy + intranet penetration)

CDN with OSS acceleration
随机推荐
Clean up photo SCR virus / iframekill injection removal /iframekill removal photo scr
Chrome page recording and playback function
How to use EEPROM in 51 Single Chip Microcomputer?
PHP splits a string into arrays
Daily tests
Your requirements could not be resolved
Install SVN in Pagoda and build SVN version Library
Nailing open platform - applet development practice (nailing applet server side)
In 2022, what professional competitions can college students majoring in automation, electrical engineering and automation participate in?
Part 4: drawing quadrilateral
线程同步之读写锁
Using jsup to extract images from interfaces
一幅脑图总结一下需求分析(工作上实际遇到的情况的补充)
Spark - 一文搞懂 parquet
微软禁止俄用户下载安装Win10/11
Sorting out the examination sites of the 13th Blue Bridge Cup single chip microcomputer objective questions
The stc-isp burning program for 51 single chip microcomputer always shows that "the target single chip microcomputer is being detected..." the cold start board does not respond
NPM installation tutorial
线程同步之互斥量(互斥锁)
Zeromq from getting started to mastering