码迷,mamicode.com
首页 > Web开发 > 详细

laravel5.5http会话机制

时间:2018-02-09 17:25:31      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:load   之间   pre   不为   agent   使用   ble   show   body   

  • 配置文件 config/session.php

大多数是用file驱动,将session保存在storage/framework/sessions,可以考虑使用redis或者memcached 驱动实现更出色的性能

  • 使用database作为驱动

需要创建数据表

php artisan session:table

php artisan migrate

数据表内容

Schema::create('sessions', function ($table) {
    $table->string('id')->unique();
    $table->integer('user_id')->nullable();
    $table->string('ip_address', 45)->nullable();
    $table->text('user_agent')->nullable();
    $table->text('payload');
    $table->integer('last_activity');
});
  • 使用session

laravel 中处理 Session 数据有两种主要方法:

全局辅助函数 session 和通过一个 Request 实例。

    public function show(Request $request, $id)
    {
        $value = $request->session()->get('key');

        //
    }
Route::get('home', function () {
    // 获取 Session 中的一条数据...
    $value = session('key');

    // 指定一个默认值...
    $value = session('key', 'default');

    // 在 Session 中存储一条数据...
    session(['key' => 'value']);
});

通过 HTTP 请求实例操作 Session 与使用全局辅助函数 session 两者之间并没有实质上的区别

  • 获取所有 Session 数据

    $data = $request->session()->all();

  • 要确定 Session 中是否存在某个值,可以使用 has 方法。如果该值存在且不为 null,那么 has 方法会返回 true:

    if ($request->session()->has('users')) {
    //
    }
  • 要确定 Session 中是否存在某个值,即使其值为 null,也可以使用 exists 方法。如果值存在,则 exists 方法返回 true:

    if ($request->session()->exists('users')) {
    //
    }

laravel5.5http会话机制

标签:load   之间   pre   不为   agent   使用   ble   show   body   

原文地址:https://www.cnblogs.com/redirect/p/8435835.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!