如何在laravel中扩展立面


How to extends a facade in laravel?

我尝试在laravel 4中扩展faceede,但在尝试调用方法时只得到下一个错误。

Non-static method App'Libraries'Theme::setActive() should not be called statically

编辑

在@Antonio的响应之后,要将方法更改为静态,请在方法中使用关键字$ this->。

Symfony ' Component ' Debug ' Exception ' fatalerroreexception Using$this不在$active = $this->ensureRegistered($active);

的对象上下文中我代码:

<?php namespace App'Libraries;
use Cartalyst'Themes'Facades'Theme as ThemeBag;
class Theme extends ThemeBag {
    /**
     * Sets the active theme.
     *
     * @param  mixed  $active
     * @return Cartalyst'Themes'ThemeInterface
     */
public static function setActive($active)
{
    $active = $this->ensureRegistered($active);
    if ( ! isset($this->themes[$active->getSlug()]))
    {
        $this->register($active);
    }
    $this->active = $active;
    include $this->getActive()->getPath() . '''helpers''composers.php';
}
}

基本上你必须扩展一个已经存在的Facade:

<?php namespace AntonioRibeiro'Libraries;
class MyEventFacade extends Illuminate'Support'Facades'Event {
    /**
     * Sets the active theme.
     *
     * @param  mixed  $active
     * @return Cartalyst'Themes'ThemeInterface
     */
    public static function setActive($active)
    {
        /// do what you have to do
    }
}

然后替换(或添加为一个新的)到app/config/app.php:

'aliases' => array(
        'App'             => 'Illuminate'Support'Facades'App',
                ...
     // 'Event'           => 'Illuminate'Support'Facades'Event',
        'Event'      => 'AntonioRibeiro'Libraries'MyEventFacade',
                ...
        'File'            => 'Illuminate'Support'Facades'File',
        'ActiveSession'   => 'AntonioRibeiro'Facades'ActiveSessionFacade',
),

别忘了执行'composer dump- autolload '。

我没有访问那些Cartalyst主题,但是你在接收的错误与你没有创建为静态的方法有关:

public function setActive($active)
{
}

应该是

public static function setActive($active)
{
}

您可以在这里找到一些很好的信息(创建一个类扩展请求"Facade"):http://fideloper.com/extend-request-response-laravel