I searched thru FuelPHP document but not found cookie prefix configuration.

I extend class in fuel/app/classes/extension/cookie.php with this code.

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'); 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?

有帮助吗?

解决方案

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.

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归因
不隶属于 StackOverflow
scroll top