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 CookieJarStock
s 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中进行判断.