Php Laravel外部带照明/容器的自动绑定分辨率

Php Laravel外部带照明/容器的自动绑定分辨率,php,laravel,ioc-container,Php,Laravel,Ioc Container,我试图介绍一些照明组件来拯救一个遗留应用程序,即容器、事件和路由器。在尝试将具体类绑定到接口时,我无法通过BindingResolutionException index.php <?php require __DIR__ . '/vendor/autoload.php'; $app = new Illuminate\Container\Container; $app->bind('dispatcher', function () { return new Illumi

我试图介绍一些照明组件来拯救一个遗留应用程序,即容器、事件和路由器。在尝试将具体类绑定到接口时,我无法通过BindingResolutionException

index.php

<?php

require __DIR__ . '/vendor/autoload.php';

$app = new Illuminate\Container\Container;

$app->bind('dispatcher', function () {
    return new Illuminate\Events\Dispatcher;
});

$app->bind('router', function ($app) {
    return new Illuminate\Routing\Router($app['dispatcher']);
});

// This is the interface I'm trying to bind
$app->bind('App\Logable', function () {
    return new App\Logger();
});

$router = $app['router'];

// This is where I'm trying to use automatic binding resolution
$router->get('/', function (App\Logable $logger) {
    return $logger->log();
});

$request = Illuminate\Http\Request::createFromGlobals();
$response = $router->dispatch($request);
$response->send();

我意识到我正在实例化多个容器,而不是传递我第一次创建的容器(通过查看对象ID)。我的解决方案是在实例化时将容器传递给路由器:

$app->bind('router', function ($app) {
    return new Illuminate\Routing\Router($app['dispatcher'], $app);
});
然后一切都如期进行

<?php

namespace App;

class Logger implements Logable
{
    public function log()
    {
        var_dump($this);
    }
}
$app->bind('router', function ($app) {
    return new Illuminate\Routing\Router($app['dispatcher'], $app);
});