2

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

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

编辑

在@Antonio 响应后,将方法更改为静态,让在方法内部使用关键字 $ 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';
}
}
4

1 回答 1

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
    }

}

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

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

public function setActive($active)
{
}

应该是

public static function setActive($active)
{
}

你会在这里找到一些很好的信息(创建一个扩展请求“外观”的类): http: //fideloper.com/extend-request-response-laravel

于 2013-06-06T04:03:56.380 回答