如何在laravel扩展立面?

Mig*_*ges 2 php laravel laravel-4

我尝试在laravel 4中扩展一个facede,但是在尝试调用方法时我只会遇到下一个错误.

Non-static method App\Libraries\Theme::setActive() should not be called statically
Run Code Online (Sandbox Code Playgroud)

编辑

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

Symfony\Component\Debug\Exception\FatalErrorException当不在对象上下文中时使用$ 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';
}
}
Run Code Online (Sandbox Code Playgroud)

Ant*_*iro 5

基本上你必须扩展现有的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
    }

}
Run Code Online (Sandbox Code Playgroud)

然后将(或将其添加为新的)替换为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',

),
Run Code Online (Sandbox Code Playgroud)

不要忘记执行'composer dump-autoload'.

我无法访问那些Cartalyst主题,但您接收的错误与您未创建的静态方法有关:

public function setActive($active)
{
}
Run Code Online (Sandbox Code Playgroud)

应该是

public static function setActive($active)
{
}
Run Code Online (Sandbox Code Playgroud)

你可以在这里找到一些关于它的好信息(制作一个扩展Request"Facade"的类):http://fideloper.com/extend-request-response-laravel