简体   繁体   English

Laravel Facade类未加载根类,而是返回未找到的方法

[英]Laravel Facade class not loading root class instead returns method not found

I am trying to get my Facade class to work, however it seems Laravel is calling the method on my Facade class instead of calling it on my root class. 我正在尝试使Facade类正常工作,但是Laravel似乎在Facade类上调用了该方法,而不是在根类上调用了该方法。 So I get method undifined error. 所以我得到方法未定义的错误。 When I create the the feed class directly from the binding ( App::make('feed')->addArticle();) it works. 当我直接从绑定(App :: make('feed')-> addArticle();)创建feed类时,它可以工作。 So I think there is soemthing wrogn with my Facade. 因此,我认为我的Facade令人发指。 Any ideas? 有任何想法吗? Thank in advance. 预先感谢。

Controller 调节器

class RssController extends BaseController 
{

    public function getArticles() {
         Feed::addArticle();
    }
}

ServiceProvider 服务提供者

use Illuminate\Support\ServiceProvider;



class FeedServiceProvider extends ServiceProvider  {    
    public function register()
    {
        $this->app->bind('feed', function()
        {
            return new Feed;
        });
    }
}

Facade class 立面课

use Illuminate\Support\Facades\Facade;

class FeedFacade extends Facade {

 protected static function getFacadeAccessor() 
 { 
    return 'feed'; 
 }

}

Root Class 根类

class Feed {

//vars


    public function __construct()
    {

    }

    public function make() {
        return new Feed();
    }

    public function addArticle() {
        return '@addArticle';

    }

The problem seems to be you want both your Laravel Facade ( Feed:: ), and the implementation class of your service provider ( class Feed ) to have the same name. 问题似乎是您希望Laravel Facade( Feed:: :)和服务提供商的实现类( class Feed )都具有相同的名称。 Facades work because in app/config/app.php there's an alias section 外墙有效,因为在app/config/app.php有一个别名部分

'aliases' => array(
    'App'             => 'Illuminate\Support\Facades\App',
    'Artisan'         => 'Illuminate\Support\Facades\Artisan',
    'Auth'            => 'Illuminate\Support\Facades\Auth',
    'Blade'           => 'Illuminate\Support\Facades\Blade',

This aliasing means whenever you use, say, the App facade 这种别名意味着您每当使用例如App Facade时

`App::someMethod`

Laravel actually invokes the getFacadeAccessor on Illuminate\\Support\\Facades\\App . Laravel实际上在Illuminate\\Support\\Facades\\App上调用getFacadeAccessor There's no global class App in the system. 系统中没有全局类App If there were it would cause a similar problem with the facade. 如果有的话,将给立面造成类似的问题。

Get an alias for Feed => Illuminate\\Support\\Facades\\Facade\\FeedFacade into your system, and get your implementation class Feed out of the global namespace (moving the file to an appropriate place) 获取Feed的别名=> Illuminate\\Support\\Facades\\Facade\\FeedFacade进入您的系统,并将实现类Feed从全局名称空间中移出(将文件移至适当的位置)

<?php
namespace My\Namespace;
class Feed
{
}

... ...

return new \My\Namespace\Feed;

and you should be all set. 而且你应该准备好了。

Also, at the risk of confusing you, you don't need to drop your own classes in the Illuminate\\Etc\\... namespace, and you should probably put them in your own unless you're trying to get the core Laravel team to accept your classes as the official feed service. 另外,冒着使您感到困惑的风险,您无需将自己的类放到Illuminate\\Etc\\...名称空间中,并且除非您试图让核心Laravel团队加入,否则您应该将它们放在自己的类中接受您的课程作为官方供稿服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM