简体   繁体   English

在Laravel中未设置会话变量

[英]Session variable isn't set in Laravel

everybody. 大家

I made a middleware. 我做了一个中间件。 I made a global one putting it into Kernel.php, which is called to every request and which verify if user is loged in. 我创建了一个全局文件,将其放入Kernel.php,该文件会被调用到每个请求中,并验证用户是否已登录。

The point was I needed to use the session for that. 关键是我需要为此使用会话。 But the session was empty. 但是会议是空的。 Probably because it is populated later in script. 可能是因为稍后在脚本中填充了它。 So I fix it using a global middleware called '\\Illuminate\\Session\\Middleware\\StartSession::class'. 因此,我使用一个名为'\\ Illuminate \\ Session \\ Middleware \\ StartSession :: class'的全局中间件对其进行了修复。 It was great because after that I could see, in my middleware, what session contains. 很棒,因为在那之后我可以在中间件中看到会话包含的内容。

But another bug showed up. 但是出现了另一个错误。 Since I puted the '\\Illuminate\\Session\\Middleware\\StartSession::class' middleware, my redirects doesn't put on session the variable anymore. 自从我放置了'\\ Illuminate \\ Session \\ Middleware \\ StartSession :: class'中间件以来,我的重定向就不再在会话中放置变量了。

Till now code below worked perfectly and redirect created the arsh variable on session: 到现在为止,下面的代码可以正常工作,并在会话上重定向创建了arsh变量:

return redirect('/admin')->with('arsh', $arsh);

But now doesn't put anymore the arsh variable on session on redirect. 但是现在不再将arsh变量放在重定向会话上。

I researched a lot on internet, but nothing. 我在互联网上进行了很多研究,但一无所获。 I saw a lot of advices but no one worked. 我看到了很多建议,但没有人起作用。

I just don't know what I can do. 我只是不知道该怎么办。 If you know something would be awesome. 如果您知道某事会很棒。

You also can think about using another solving method for reading session in my middleware, and probably this won't need anymore thinking about last bug. 您也可以考虑使用另一种解决方法来读取我的中间件中的会话,并且可能不再需要考虑上一个错误。

I hope you understood what I wrote and sorry for my english. 我希望你能理解我写的东西,并为我的英语感到抱歉。

Edit: 编辑:

I did what Hamoud said: I moved my middleware (\\App\\Http\\Middleware\\RedirectIfNotAuthenticated::class,) from $middleware to $middlewareGroups: 我做了Hamoud所说的:我将中间件(\\ App \\ Http \\ Middleware \\ RedirectIfNotAuthenticated :: class,)从$ middleware移到了$ middlewareGroups:

protected $middleware = [
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
    \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
    \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
    \App\Http\Middleware\TrimStrings::class,
];

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,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
        \App\Http\Middleware\RedirectIfNotAuthenticated::class, // my middleware
    ],

    'api' => [
        'throttle:60,1',
        'bindings',
    ],
];

And now redirect works well and can sets variables on session. 现在重定向很有效,可以在会话上设置变量。 But my middleware doesn't work anymore: 但是我的中间件不再起作用了:

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;
use Session;
use Request;

class RedirectIfNotAuthenticated
{
    /**
     * handle
    */
    public function handle($request, Closure $next, $guard = null)
    {   
        if ($request->is('admin/*') && !$request->is('admin') && !$request->is('admin/login') && (!Session::has(_DB_PREFIX_.'id_admin') || !Session::has(_DB_PREFIX_.'name_admin'))) {
            return redirect('/admin');
        }

        return $next($request);
    }
}

If statement is never true... That because $middleware is called before verifying the route, while $middlewareGroups is called after. 如果语句永远不是真的……那是因为$ middleware在验证路由之前被调用,而$ middlewareGroups在之后被调用。 So I am redirect to 404 when my url is something like domain.com/admin/fgtsdr. 因此,当我的网址是domain.com/admin/fgtsdr之类的内容时,我将重定向到404。

What my middleware does is if route is admin/* and I am not loged in, it redirects me to /admin. 我的中间件所做的是,如果route是admin / *并且我没有登录,它将我重定向到/ admin。 The point is it has to do that even if route exists or not... 关键是即使路线不存在也必须这样做...

You did not post your Kernel.php file, and it's not clear what do mean by global middleware. 您没有发布Kernel.php文件,也不清楚全局中间件的含义。 Do you add it the $middleware array or $middlewareGroups array? 您将其添加到$middleware数组还是$middlewareGroups数组?

The order of these middleware matters. 这些中间件的顺序很重要。 In the Kernel.php file there are three arrays in which you can register your middleware. Kernel.php文件中,您可以在三个数组中注册中间件。

  1. $middleware . $middleware To be triggered with every request. 被每个请求触发。

  2. $middlewareGroups . $middlewareGroups You register group of middleware to assign to specific route. 您注册一组中间件以分配给特定的路由。 It has two main groups: web and api . 它有两个主要组: webapi The web group is assigned to all routes registered in the routes/web.php file. web组被分配给在routes/web.php文件中注册的所有路由。

  3. $routeMiddleware . $routeMiddleware Individual middleware to assign to specific route. 各个中间件分配给特定的路由。

I assume you started by putting your custom middleware in the $middleware to make it global, and when this did not work you added Laravel's StartSession middleware to the same array. 我假设您首先将自定义中间件放入$middleware以使其具有全局性,并且当此方法不起作用时,您将Laravel的StartSession中间件添加到了同一阵列中。 However, StartSession is already registered in the $middlewareGroups array within the web group. 但是, StartSession已在web组的$middlewareGroups数组中注册。 So, you have two sessions one of them is destroying the other. 因此,您有两个会话,其中一个正在销毁另一个会话。

The correct way to add a middleware to all routes, when they need a session (web routes), is to add it in the web group if the $middlewareGroups after the StartSession middleware. 将中间件添加到所有路由(当它们需要会话(Web路由)时)的正确方法是,如果$middlewareGroups StartSession中间件之后StartSession其添加到web组中。

For example, 例如,

protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class, <---- Laravel session middleware
        // \Illuminate\Session\Middleware\AuthenticateSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,

        \App\Http\Middleware\CustomMiddleware::class, <--- Your global middleware. 
    ],

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM