1

I want to extend Laravel5 Cookies functionality. I want to make it this way: I will create file App\Support\Facades\Cookie.php and than file App\Libraries\CookieJar.php. In app.php I will change row for Cookie to this:

'Cookie' => 'App\Support\Facades\Cookie',

Anyway, when I try to use it like this:

Cookie::test()

it returns:

Call to undefined method Illuminate\Cookie\CookieJar::test()

Do you have any idea, why it do this? And is the way, how I want to extend Cookie functionality good?

Thank you for your help.

Here is content of files: Cookie.php:

<?php namespace App\Support\Facades;

/**
 * @see \App\Libraries\CookieJar
 */
class Cookie extends \Illuminate\Support\Facades\Facade
{

    /**
     * Determine if a cookie exists on the request.
     *
     * @param  string $key
     * @return bool
     */
    public static function has($key)
    {
        return !is_null(static::$app['request']->cookie($key, null));
    }

    /**
     * Retrieve a cookie from the request.
     *
     * @param  string $key
     * @param  mixed $default
     * @return string
     */
    public static function get($key = null, $default = null)
    {
        return static::$app['request']->cookie($key, $default);
    }

    /**
     * Get the registered name of the component.
     *
     * @return string
     */
    protected static function getFacadeAccessor()
    {
        return 'cookie';
    }

}

CookieJar.php:

<?php namespace App\Libraries;

class CookieJar extends \Illuminate\Cookie\CookieJar
{
    public function test() {
        return 'shit';
    }

}
Jan Kožušník
  • 683
  • 3
  • 16
  • 30

1 Answers1

1

The class with all your new cookie functions need to extend Illuminate\CookieJar\CookieJar

<?php 

namespace App\Support\Cookie;

class CookieJar extends \Illuminate\Cookie\CookieJar
{

    /**
     * Determine if a cookie exists on the request.
     *
     * @param  string $key
     * @return bool
     */
    public static function has($key)
    {
        return !is_null(static::$app['request']->cookie($key, null));
    }

    /**
     * Retrieve a cookie from the request.
     *
     * @param  string $key
     * @param  mixed $default
     * @return string
     */
    public static function get($key = null, $default = null)
    {
        return static::$app['request']->cookie($key, $default);
    }

}

Then make a new facade:

namespace App\Support\Facades;

class CookieFacade extends \Illuminate\Support\Facades\Facade
{

    protected static function getFacadeAccessor()    
    {
        /*
         * You can't call it cookie or else it will clash with
         * the original cookie class in the container.
         */
        return 'NewCookie';
    }
}

Now bing it in the container:

$this->app->bind("NewCookie", function() {
    $this->app->make("App\\Support\\Cookie\\CookieJar");
});

Finally add the alias in your app.php config:

'NewCookie' => App\Support\Facades\CookieFacade::class

Now you can use NewCookie::get('cookie') and NewCookie::has('cookie').

I hope this helps.

Dastur
  • 684
  • 6
  • 23