当前位置:网站首页>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 .
边栏推荐
- 35岁程序员炒Luna 千万资产3天归零,网友:和赌博一样
- China air compressor manufacturing market demand analysis and investment prospect research report 2022-2028
- Report on demand situation and development trend of China's OTC industry from 2022 to 2028
- I/o virtualization technology - UIO framework
- 【QT】资源文件导入
- 軟件調試測試的十大重要基本准則
- 6、 Project practice --- identifying cats and dogs
- Development trend and prospect forecast report of China's financial industry 2022-2028 Edition
- pip 批量完全卸载包
- Gateway can not connect to tcp://127.0.0.1: Connection refused
猜你喜欢

1. foundation closing

Implementation of seven classes of BlockingQueue interface

Mysql8.0 configuring my SQL in INI file_ mode=NO_ AUTO_ CREATE_ User can start
![[Qunhui] this suite requires you to start PgSQL adapter service](/img/fb/1aea7eb833afc1a24531b612a98400.jpg)
[Qunhui] this suite requires you to start PgSQL adapter service

What should I do if the 51 SCM board cannot find the device in keil

35岁程序员炒Luna 千万资产3天归零,网友:和赌博一样

Tp6 multi table Association (table a is associated with table B, table B is associated with table C, and table d)

Tencent Interviewer: How did binder get its system services?

CTF PHP audit bypasses filtering learning from topics
![ctf [RoarCTF 2019]easy_ calc](/img/c7/16adb8e4b64a4be2129a6c53c5aaa7.jpg)
ctf [RoarCTF 2019]easy_ calc
随机推荐
一幅脑图总结一下需求分析(工作上实际遇到的情况的补充)
钉钉开放平台-小程序开发实战(钉钉小程序客户端)
OSS CDN alicloud configuration method
Composer version rollback version switching
WPF 值转换
Getter actual combat geTx tool class encapsulation -getutils
After four years of outsourcing, people are directly abandoned...
Database related knowledge
SQL related knowledge - constraints
What preparations should be made to develop an app from scratch
Understand CGI and fastcgi
What if the serial port fails to open when the SCM uses stc-isp to download software?
asp. Net web page, ASP connects to the database, and uses asp:panel and asp:dropdownlist controls
Zhubo Huangyu: all the precious metals you want to know are here
钉钉开放平台-小程序开发实战(钉钉小程序服务器端)
Threejs专用天空盒素材,五种天空盒素材免费下载
[Qunhui] command line acme SH automatically apply for domain name certificate
Matplotlib multi line chart, dot scatter chart
Lua语法讲解
Knowledge of functions