문제

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