douyou8266 2014-05-14 06:00
浏览 30
已采纳

模型失败的Laravel外观:无法重新声明类内容

I want to add a Facade to my model named Content. But I always get the error Cannot redeclare class Content. Is that because the model is already loaded via the autoload? How can I fix this?

I appreciate you help, thanks.

This is how my composer file looks like

"autoload": {
    "psr-4": {
        "App\\": "app/"
    },
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/models",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php",
        "app/Serviceproviders"
    ]
},

And this is how my Serviceprovider looks like.

<?php namespace App\Serviceproviders;

use Illuminate\Support\ServiceProvider;

class FormandsystemServiceProvider extends ServiceProvider {

    public function register()
    {
        $this->app->bind('content', function()
        {
            return new \App\models\Content;
        });

          $this->app->booting(function()
          {
            $loader = \Illuminate\Foundation\AliasLoader::getInstance();
            $loader->alias('Content', '\App\Facades\Content');
          });
    }

}

My Facade looks like this.

<? namespace App\Facades;

use Illuminate\Support\Facades\Facade;

class Content extends Facade {

    protected static function getFacadeAccessor() { return 'content'; }
}
  • 写回答

1条回答 默认 最新

  • douwu5009 2014-05-15 06:42
    关注

    Okay, I got it, the Problem seems to be that both facade and model are named the same, which is unexpected, because it works just like this with packages but I guess this might be because of those not being autoloaded?

    Anyway, my "solution" is the following:

    1. Removing "app/Serviceproviders" from composer.json as I am loading this via app.php anyway.

    2. Renaming my mode ContentModel and the file ContentModel.php because I want my Facade to be Content. This lives in the standard model folder.

    3. My facade is still named Content and the file is content.php which lives in app/Facades

    4. This is my Serviceprovider which is loaded using the app.php file

    use Illuminate\Support\ServiceProvider;

    class FormandsystemServiceProvider extends ServiceProvider {

    public function register()
    {
        $this->app->bind('content', function()
        {
            return new \App\models\ContentModel;
        });
    
          $this->app->booting(function()
          {
            $loader = \Illuminate\Foundation\AliasLoader::getInstance();
            $loader->alias('Content', '\App\Facades\Content');
          });
        }
    }
    

    This works just fine I can now call Content::getFirst() which is a method from my content model.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度