我正在研究Laravel 5.4.30.

假设我们有一个域example.com和一个子域dev.example.com.主域用于主分支,开发子域用于开发分支.我们有一个cookie通知系统,点击Hide Cookie Notice按钮后会被隐藏.这是通过永远设置cookie来实现的.

对于主域:

SESSION_DOMAIN=example.com

对于dev子域:

SESSION_DOMAIN=dev.example.com

Now the issue comes from here. If we go to the example.com and click on hiding the cookie notice, a cookie will be set forever for main domain. After that we go to the dev.example.com and do the same. So a cookie will be set for subdomain as well. But this cookie has been set after previous one. (The order is important) Now if we refresh the subdomain, we will see that notice again! (not hidden) The browser has read the main cookie because of .example.com set in domain parameter of cookie in the browser, so every subdomain will be affected. But the view still shows the notice because it cannot read any cookie for hiding.

无论如何,我不想在所有的子域之间共享该cookie.我怎样才能做到这一点呢?我想我应该在曲奇名称前加上一个前缀.但是我不知道怎么做,那只小腊肠会自动在曲奇名称上加前缀.

Any solutions?

推荐答案

You need to implement your own "retrieving" and "setting" a cookie.

检索(HAS、GET)Cookie

用名字Cookie创建自己的新类(任何你喜欢的地方,但我会做app/Foundation/Facades/个).

use \Illuminate\Support\Facades\Cookie as CookieStock;

class Cookie extends CookieStock { 
    //implement your own has(...);
    public static function has($key)
    {
        return ! is_null(static::$app['request']->cookie(PREFIX . $key, null)); //get the prefix from .env file for your case APP_ENV
    }

    //implement your own get(...);
    public static function get($key = null, $default = null) {...}
}

现在打开config/app.php并更改相应的别名(cookie).

设置(制作)cookies

Create yourself new provider (use artisan), and copy-paste code from Illuminate\Cookie\CookieServiceProvider.php and change namespaces. Again open up config/app.php and change corresponding service provider with the new one.

创建名为CookieJar的新类(您喜欢的任何地方,但我会创建app/Foundation/Cookie/个).

use \Illuminate\Cookie\CookieJar as CookieJarStock;

class CookieJar extends CookieJarStock {

//Override any method you think is relevant (my guess is make(), I am not sure at the moment about queue related methods)

    public function make($name, $value, $minutes = 0, $path = null, $domain = null, $secure = false, $httpOnly = true)
    {
        // check before applying the PREFIX
        if (!empty($name)) {
            $name = PREFIX . $name; // get the PREFIX same way as before
        }

        return parent::make($name, $value, $minutes, $path, $domain, $secure, $httpOnly);
    }
}

使现代化 the code in your own cookie service provider to use your implementation of CookieJar (line 19).

Run $ composer dump-autoload, and you should be done.

使现代化

Since BorisD.Teoharov brought up, that if framework changes signature of CookieJarStocks make() (or any other cookie related function) in between the major versions, I made a example repository, that includes a test that can be used as is and it will fail if signature change happens.

It is as simple as this:

public function test_custom_cookie_jar_can_be_resolved()
{
    resolve(\App\Foundation\Cookie\CookieJar::class);
    $this->assertTrue(true);
}

详细说明了如何可以在the corresponding commit diff中进行判断.

Laravel相关问答推荐

当使用Craftable PRO时,Wysiwig是什么时候生成的?

AJAX响应中未定义的全名

按回车键时如何防止此功能运行?

Laravel Horizo​​n 限制与优化

spatie 包 laravel-tags 在迁移中没有 down() 函数是有原因的吗

Laravel 集合排序不生效

指令allow_call_time_pass_reference警告

判断 Laravel 模型表中是否存在列,然后应用条件

如何更改 Laravel5 中的视图文件夹?

Laravel 5.4 LengthAwarePaginator

如何使用 ajax 请求删除 laravel 5.3 中的记录?

将 Laravel .env 变量添加到 Vue 组件

在 Eloquent 中按自定义顺序对集合进行排序

如果用户未登录 Laravel,则重定向到登录

Laravel/PHPUnit:断言 json 元素存在而不定义值

Laravel php artisan 产生错误

Laravel 5 - 跳过迁移

Laravel 5 new auth:获取当前用户以及如何实现角色?

在哪里放置 Blade::extend

Laravel:Redis 无法建立连接:[tcp://127.0.0.1:6379]