Laravel - 5.3 major changes to the middleware execution process!
伊谢尔伦
伊谢尔伦 2017-05-16 16:49:21
0
2
801

No one in China is using the latest version, so they haven’t noticed this logical change? Didn't you check the user's login in the constructor (you're in trouble if you didn't notice)?
Help me read this official reply: https://github.com/laravel/fr...
**My English is not good! please explain! A very important improvement in laravel5.3
Why do you do this?
How should I write new logic? **
The reason why I need this improvement, and how I can solve my logic!

Question: This will invalidate __construct
Then it is more reasonable that the middleware except should be added to the middleware as an attribute!
protected $except =['login','register','oauth_callback'];
Tangled: It feels unreasonable to add this (it would be unreasonable if multiple groups call this), so don't add this. It feels bad to write __construct once logged in!

Current answer: The one on the first floor is actually OK, but this one is much more elegant
callAction description: https://laravel.com/api/maste...

laravel 5.3, it is the latest anyway, updated every time!
Now there are these 3, middleware, controller, constructor
There are 2 functions that must be instantiated in the constructor. And it must be logged in. Instantiation is not allowed without logging in.
Constructor:

middleware:

method:

Output:


routing


Custom middleware

According to laravel’s logical output:
2 Constructor
1 Middleware
3 This is promotion

But in this case, the constructor is instantiated without logging in, and no matter where the middleware is placed, the constructor will be run first and then the middleware.
The result I want is:
1 Middle File, determine login, jump to
2 constructor if not logged in, instantiate after login
3 This is promotion, execute promotion!
How to modify the logic? I have to log in before calling the public method. If I am not logged in, I will jump to login (excluding registration and login)!

The general problem is that laravel must run the constructor first, and then the middleware can be called.
Then what kind of logic should I use to achieve my requirements?

$this->wx_api();
$this->agent();

These two common ones are usually written in __construct!

I am still confused. According to common sense, if it is defined in the router, the judgment class has this method, and then the middleware is called at this time, and then the constructor is executed, and then the method! How could such a process happen!


Regarding the rough method, see the following (you cannot jump in the constructor, and writing echo and exit in laravel will be super ugly. Anyway, I can’t stand it. The above demonstration is purely for viewing!)

http://laravelacademy.org/pos...
Thinking about it carefully, I found that this perfectly destroyed the requirement of object-oriented separation of responsibilities and successfully improved the degree of coupling
Because of this In short, I just want to use middleware! (Which one is better? I am a newbie and I don’t even know how to say it!)


Laravel 5.3 is a method of constructing Middleware -> Constructing Controller -> Executing middleware handle -> Executing Controller middleware -> Controller, so the middleware is executed after the constructor

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(2)
迷茫

I am using the latest version 5.3 in China

5.3 has indeed changed the implementation logic of middleware

路由匹配 - 读取路由中间件 - 实例化Controller - 读取Controller中间键 - 执行中间件 - 执行action

Personally, it is not encouraged to initialize methods in the constructor of Controller. Do not do any logical judgment except using middleware calls.

重写 CallAction 在 CallAction中逻辑判断

Because the matching of any route will use CallActionto call the method in the Controller

In versions below 5.3, the middleware will be executed first, and then the Controller will be initialized. This is the biggest difference from 5.3.

淡淡烟草味

The constructor should be executed first, so there seems to be nothing wrong with it. . .

I looked through the documentation of laravel5.3. Now middleware is executed after construction. Either you switch to other lower versions, or you don’t use middleware, or you write all the methods to be called by your original constructor in the middleware. .

For example: (Note: This feature requires laravel version 5.3.4 or above)

public function __construct() {

    $this->middleware(function ($request, $next) {
        $this->wx_api();
        return $next($request);
    });
}

public function wx_api () {
    
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template