Confusion about Laravel Session, hope experts can solve it

WBOY
Release: 2016-08-26 10:12:54
Original
876 people have browsed it

In laravel5.*, session start is placed in the web middleware, such as

<code>    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,   //<---------
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
        ],
        'api' => [
            'throttle:60,1',
            'bindings',
        ],
    ];</code>
Copy after login

And according to the code of Route loading middleware, we can know that the Controller only calls the middleware after being made

<code>    /**
     * Get the middleware for the route's controller.
     *
     * @return array
     */
    public function controllerMiddleware()
    {
        if (! $this->isControllerAction()) {
            return [];
        }

        return ControllerDispatcher::getMiddleware(
            $this->getController(), $this->getControllerMethod()
        );
    }</code>
Copy after login

The problem comes at this time. I have a BaseController. In the constructor, the user login status will be determined. If the user is already logged in, the logged in user information will be obtained and saved to $this->login_user_info for subclass calls. If you make controller first, The session has not started yet, so the session_id of the logged in user cannot be obtained in the constructor. Part of the code is as follows

<code>/**
 * 控制层公有方法集合
 * Class BaseController
 */
abstract class BaseController extends Controller
{

    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public $login_user_info;
    public $login_subuser_info;

    public function __construct()
    {
        $this->userModel    = app(UserModel::class);
        if (session()->get('user_id')) {
            $this->login_user_info          = $this->userModel->getLoginUser();
            //设置模板全局变量
            view()->share(['login_user_info' => $this->login_user_info]);
        }
    }</code>
Copy after login

I have tested that the session can be obtained in the middleware, because the StartSession middleware code has been executed at this time. As for why I want to do this, it is a long story. My project is an old project and the framework is switched to laravel, so in order to maximize The original logic is maintained to the maximum extent, and there are some strange writing methods without using Auth. Regardless of these for now, is there a way to get the session in the constructor? Please help me, thank you

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template