douxiong5972 2017-04-05 19:45
浏览 20

Laravel - 服务提供商:未找到类

I started a project on Laravel 5.4 today and got a ServiceProvider problem. Here my service provider :

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class WizamProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //die('YESSS');
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
      $this->app->bind('Wizam\Test', function()
      {
        if(class_exists("Domains\Domomat\Test"))
          return new \Domains\Domomat\Test;
        else
          return new \Core\Classes\Test;
      });
    }
}

I added this provider into config/app.php (App\Providers\WizamProvider::class), dump my autoloader like twenty times, clear cache, clear config. Nothing happened.

Here my route :

Route::get('/sub', function()
{
  $test = new \Wizam\Test();
  echo $test->render();
});

When I go to '/sub', I got Class 'Wizam\Test' not found. I cannot see my error, can you help me ?

Thanks !

  • 写回答

1条回答 默认 最新

  • donglu9896 2017-04-05 20:24
    关注

    To use the container you cannot instantiate using the new command, you can either inject it through the constructor

    __constructor(\Wizam\Test $test)
    {
    }
    

    or using the app(\Wizam\Test::class) I believe is the other way to do it as mentioned in the comments.

    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法