Laravel 5 Laravel5延伸立面
我想扩展Laravel5 Cookies的功能。 我想这样做: 我将创建App\Support\Facades\Cookie.php文件,而不是App\Libraries\CookieJar.php文件。在app.php中,我将Cookie的行更改为:Laravel 5 Laravel5延伸立面,laravel-5,laravel-facade,Laravel 5,Laravel Facade,我想扩展Laravel5 Cookies的功能。 我想这样做: 我将创建App\Support\Facades\Cookie.php文件,而不是App\Libraries\CookieJar.php文件。在app.php中,我将Cookie的行更改为: 'Cookie' => 'App\Support\Facades\Cookie', 不管怎样,当我尝试这样使用它时: Cookie::test() 它返回: 调用未定义的方法illumb\Cookie\CookieJar::test()
'Cookie' => 'App\Support\Facades\Cookie',
不管怎样,当我尝试这样使用它时:
Cookie::test()
它返回:
调用未定义的方法illumb\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';
}
}
包含所有新cookie函数的类需要扩展illighted\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);
}
}
现在将其放入容器中:
$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')
我希望这有帮助
$this->app->bind("NewCookie", function() {
$this->app->make("App\\Support\\Cookie\\CookieJar");
});
'NewCookie' => App\Support\Facades\CookieFacade::class