Sunday, March 5, 2017

Laravel - Service Provider - Error 500

I can't get a service provider that doesn't throw an Error 500. I assume that it should be a namespace problem ?

I have a controller :

class GameController extends Controller
{
    public function start(GameRepositoryInterface $gameRepository) {
        return $gameRepository->getLastAdd();
    }
}

Which use this ServiceProvider :

namespace App\Providers\Game;

use App\Http\Controllers\Game\GameRepositoryInterface;
use App\Http\Controllers\Game\StdGameRepository;
use Illuminate\Support\ServiceProvider;

class GameServiceProvider extends ServiceProvider
{
    protected $defer = true;

    public function register() {
        $this->app->bind(StdGameRepository::class, function() {
           return new StdGameRepository();
        });

        $this->app->bind(GameRepositoryInterface::class, StdGameRepository::class);
    }

    public function provides() {
        return [GameRepositoryInterface::class];
    }
}

But this doesn't work. I'm just experimenting, so I now I don't need all of this.

But this throw an error 500.

And this doesn't :

namespace App\Http\Controllers\Game;

use App;
use App\Http\Controllers\Controller;

class GameController extends Controller
{
    public function start() {
        App::bind(StdGameRepository::class, function() {
            return new StdGameRepository();
        });

        App::bind(GameRepositoryInterface::class, StdGameRepository::class);

        $gameRepository = App::make(GameRepositoryInterface::class);

        return $gameRepository->getLastAdd();
    }
}

I added the Service Provider to app.php . I activated all the debug options but I only got a white screen.

The arborescence of the project : Arborescence



via Thomas Petillot

Advertisement