【问题标题】:Laravel5 extend FacadeLaravel5 扩展门面
【发布时间】:2015-11-18 21:35:09
【问题描述】:

我想扩展 Laravel5 Cookies 功能。 我想这样做: 我将创建文件 App\Support\Facades\Cookie.php 和文件 App\Libraries\CookieJar.php。在 app.php 中,我会将 Cookie 的行更改为:

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

无论如何,当我尝试这样使用它时:

Cookie::test()

它返回:

调用未定义的方法 Illuminate\Cookie\CookieJar::test()

你有什么想法,为什么要这样做?以及如何扩展 Cookie 功能的方式好吗?

感谢您的帮助。

以下是文件内容: 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';
    }

}

【问题讨论】:

    标签: laravel-5 laravel-facade


    【解决方案1】:

    包含所有新 cookie 函数的类需要扩展 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);
        }
    
    }
    

    然后做一个新的门面:

    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';
        }
    }
    

    现在将它放入容器中:

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

    最后在你的 app.php 配置中添加别名:

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

    现在您可以使用NewCookie::get('cookie')NewCookie::has('cookie')

    我希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-13
      • 2010-10-28
      • 1970-01-01
      • 2010-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多