简体   繁体   English

FuelPHP无法设置cookie前缀

[英]FuelPHP cannot set cookie prefix

I searched thru FuelPHP document but not found cookie prefix configuration. 我通过FuelPHP文档搜索但未找到cookie前缀配置。

I extend class in fuel/app/classes/extension/cookie.php with this code. 我使用此代码在fuel / app / classes / extension / cookie.php中扩展了类。

namespace Extension;

class Cookie extends \Fuel\Core\Cookie 
{


    private static $config = array(
        'expiration'            => 0,
        'path'                  => '/',
        'domain'                => null,
        'secure'                => false,
        'http_only'             => false,
        'prefix' => '', // added prefix to cookie.
    );


    public static function _init()
    {
        static::$config = array_merge(static::$config, \Fuel\Core\Config::get('cookie', array()));
    }


    public static function set($name, $value, $expiration = null, $path = null, $domain = null, $secure = null, $http_only = null)
    {
        // add prefix to cookie.
        $prefix = '';
        is_null($prefix) and $prefix = static::$config['prefix'];
        $name = $prefix . $name;

        parent::set($name, $value, $expiration, $path, $domain, $secure, $http_only);

    }


}

When i call \\Extension\\Cookie::set('name', 'value'); 当我调用\\ Extension \\ Cookie :: set('name','value'); It returns error. 它返回错误。

Cannot access private property Extension\Cookie::$config
COREPATH/classes/cookie.php @ line 92
Line 92 is_null($expiration) and $expiration = static::$config['expiration'];

How to extend cookie class to automatic add name prefix on set, get, and delete? 如何在设置,获取和删除时将cookie类扩展为自动添加名称前缀?

It is a typo, in FuelPHP, class properties should never be defined as private , since that will interfere with the extendability, as you have noticed. 这是一个拼写错误,在FuelPHP中,类属性永远不应该被定义为private ,因为这会干扰可扩展性,正如您所注意到的那样。

It has been corrected in the current develop branch. 它已在当前的开发分支中得到纠正。

You don't need to copy and paste any code. 您无需复制和粘贴任何代码。 You can extend the needed methods and do something like this: 您可以扩展所需的方法并执行以下操作:

public static function set($name, $value, $expiration = null, $path = null, $domain = null, $secure = null, $http_only = null)
{
    $name = '<my prefix here>' . $name;
    parent::set($name, $value, $expiration, $path, $domain, $secure, $http_only);
}

The prefix is of course loaded using whatever method you want and you can use this method of prefixing to add the prefix to the needed methods. 前缀当然是使用您想要的任何方法加载的,您可以使用这种前缀方法将前缀添加到所需的方法中。

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

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