Laravel5.5 保存了 session 当时可以获取,下次请求时 session 全都是空值

 protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
        \App\Http\Middleware\TrustProxies::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
    ];
    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            \Spatie\Pjax\Middleware\FilterIfPjax::class,
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            // \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            // \App\Http\Middleware\VerifyCsrfToken::class, //关闭csrf防御
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],
        'api' => [
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            'throttle:10000,1',
            'bindings',
        ],
    ];

kernel都加上了sessionStart,
路由也在 中间件里

Route::group(['prefix' => 'h5', 'middleware' => 'web'], function () {
Route::get('/test', function () {                                              
   // print_r(session()->all());exit;  
   //使用上面的代码打印出来 只有 :
  // Array ( [_token] => tzU6L8G1IQtcGrELZaEjaDFRVlHspWYTWHOfsAp4 )
        session()->put('yy','999');
        session()->save();        
        // print_r(session()->all());exit;   //当时保存的时候是可以保存的上的
        //打印结果:
            //Array ( [_token] => b5Y5Cog2OvjdiMHmPJV0T13Y87khWJ3EWMztFfQr [yy] => 999 [_flash] => Array ( [old] => Array ( ) [new] => Array ( ) ) )
            //但是下次请求就没有了
    }); 
}

后面也没用exit,die等等中止输出的方法,我推测是中间件没派上用场,但不知道怎么去验证,第一次提问,希望能够得到大神的解答

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
讨论数量: 15
DianWang

建议看文档

4年前 评论
arunfung

看下env使用的是什么驱动,然后确认是否真的写入到对应的驱动当中.写入文件就到文件中看,写入redis就到redis中看,先确定是否真的持久化了。如果是分布式系统,就不能使用file驱动,得使用redis驱动

4年前 评论
arunfung

@yumi001 但这看起来都是laravel自带的缓存,没有看到你存的key yy

4年前 评论
arunfung

@yumi001

file

一步步debug,先从env开始,然后取消web中间件,自己手动开启session,后面再尝试cache去操作试试,做排除法,去定位问题。

4年前 评论

@phpstack

use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * This namespace is applied to your controller routes.
     *
     * In addition, it is set as the URL generator's root namespace.
     *
     * @var string
     */
    protected $namespace = 'App\Api\Controllers';

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * [@return](https://learnku.com/users/31554) void
     */
    public function boot()
    {
        //

        parent::boot();
    }

    /**
     * Define the routes for the application.
     *
     * [@return](https://learnku.com/users/31554) void
     */
    public function map()
    {
        $this->mapApiRoutes();

        $this->mapApiMRoutes();

        $this->mapH5Routes();

        $this->mapH5MRoutes();

        //
    }

    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * [@return](https://learnku.com/users/31554) void
     */
    protected function mapApiMRoutes()
    {
        Route::middleware('web')
            ->namespace($this->namespace)
            ->group(base_path('routes/web.php'));
    }

    /**
     * Define the "api" routes for the application.
     *
     * These routes are typically stateless.
     *
     * [@return](https://learnku.com/users/31554) void
     */
    protected function mapApiRoutes()
    {
        Route::prefix('api')
            ->middleware('api')
            ->namespace($this->namespace)
            ->group(base_path('app/Api/routes.php'));
    }

    /**
     * Define the "api" routes for the application.
     *
     * These routes are typically stateless.
     *
     * [@return](https://learnku.com/users/31554) void
     */
    protected function mapH5Routes()
    {
        Route::prefix('h5')
            ->middleware('h5')
            ->namespace('App\H5\Controllers')
            ->group(base_path('app/H5/routes.php'));
    }

    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * [@return](https://learnku.com/users/31554) void
     */
    protected function mapH5MRoutes()
    {
        Route::middleware('h5')
            ->namespace('App\H5\Controllers')
            ->group(base_path('app/H5/routes.php'));
    }

}

写在自定义的 app/H5/routes.php里

4年前 评论

@yumi001 最近我们写的一个项目调试的时候出现过这个问题。你试试第一次访问路由写session的时候不要有任何输出,第二次访问直接打印所有session

4年前 评论

@phpstack 您说的那种办法我最初试过,而且网上搜到能用的办法我都试了一遍,没有什么效果,我本地的环境session可以正常执行,只是git传到linux上时就各种获取不到值,我待会儿再一步步调试下吧。谢谢@arunfung @phpstack 解答

4年前 评论
lji123123

看你的情况像是cookie没有带过去

4年前 评论

@lji123123 刚刚用cookie试过了,本地环境依旧好使,一到linux环境跟session一模一样

4年前 评论

而且我发现在vendor下执行session可以保存成功,下次都可以访问的到

4年前 评论
arunfung

@yumi001 多用idea的代码格式化,保存一下就格式化一下,这种真心不容易发现,代码检测工具也可以检测出这些问题。

4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!