如何在laravel扩展立面?

时间:2013-06-06 03:58:31

标签: php laravel laravel-4

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

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

修改

在回复@Antonio后,要将方法更改为静态,请使用关键字$ this->在方法内部。

Symfony \ Component \ Debug \ Exception \ FatalErrorException使用 $ {不在$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';
}
}

1 个答案:

答案 0 :(得分: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)
{
}

Shoud be

public static function setActive($active)
{
}

你会在这里找到一些关于它的好信息(制作一个扩展请求“Facade”的课程):http://fideloper.com/extend-request-response-laravel