0

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 !

Treast
  • 1,095
  • 1
  • 9
  • 20
  • Did you setup autoloading for your classes in your `composer.json`? Also, if you want to use the container call it like this instead: `app(\Wizam\Test::class)` – Roj Vroemen Apr 05 '17 at 19:54
  • For existing classes yes ! I didn't do it for \Wizam because it doesn't technically exist. – Treast Apr 05 '17 at 19:56
  • THANK YOU !! `app(\Wizam\Test::class)` works just fine !!! – Treast Apr 05 '17 at 19:57

2 Answers2

0

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.

James
  • 671
  • 5
  • 17
0

If someone once will get error saying that your interface not found ('class not found'), during making service provider closure, make sure your interface and class located in app/ folder. It took a lot of time fixing this. Have a nice day.
P.S. answer is not related to topic, but this page is first I found when I started to google the solution, so I beg you leave this here.

B. Go
  • 1,436
  • 4
  • 15
  • 22
nojitsi
  • 351
  • 3
  • 13