thephpleague / route

Fast PSR-7 based routing and dispatch component including PSR-15 middleware, built on top of FastRoute.
http://route.thephpleague.com
MIT License
651 stars 126 forks source link

DI with League\Container = Uncaught NotFoundException in ArgumentResolverTrait.php:79 #286

Closed zenlord closed 3 years ago

zenlord commented 4 years ago

Hi, just started using both League\Container and League\Route and have been on this for the entire weekend, but still fail. This error is one that I don't know how to tackle:

<b>Fatal error</b>:  Uncaught League\Container\Exception\NotFoundException: Unable to resolve a value for parameter (dsn) in the function/method (__construct) in [...]/vendor/league/container/src/Argument/ArgumentResolverTrait.php:79
Stack trace:
#0 [internal function]: League\Container\ReflectionContainer-&gt;League\Container\Argument\{closure}(Object(ReflectionParameter))
#1  [...]/vendor/league/container/src/Argument/ArgumentResolverTrait.php(84): array_map(Object(Closure), Array)
#2  [...]/vendor/league/container/src/ReflectionContainer.php(52): League\Container\ReflectionContainer-&gt;reflectArguments(Object(ReflectionMethod), Array)
#3  [...]/vendor/league/container/src/Container.php(183): League\Container\ReflectionContainer-&gt;get('Aura\\Sql\\Extend...')
#4  [...]/vendor/league/container/src/Argument/ArgumentResolverTrait.php(45): League\Container\Container-&gt;get('Aura\\Sql\\Extend...')
#5  [...]/vendor/league/container/src/Argument/ArgumentResolverTrait.php(86): League\Contai in <b> [...]/vendor/league/container/src/Argument/ArgumentResolverTrait.php</b> on line <b>79</b>

The parameter $dsn is required to construct the Aura\Sql\ExtendedPdo object. However, I can succesfully get the ExtendedPdo object from the container and I can even get a model instance in which the ExtendedPdo object is injected via the constructor. As such, I don't think the ExtendedPdo construction nor the League\Container setup is incorrect - I seem to fail at glueing everything together in League\Route.

These are the excerpts I believe to be relevant when setting up the Route ('dbpro' is an alias for a lazy loaded ExtendedPdo object - The ServiceProvider is an exact copy/paste from the docs, in which I replaced Zend with Laminas):

$container = new League\Container\Container;
$container->delegate( new League\Container\ReflectionContainer );

$container->add('modl_party', App\Model\Party::class)->addArgument('dbpro');
$container->add('ctrl_party', App\Controllers\PartyController::class)->addArgument('modl_party');
$container
    ->addServiceProvider('App\ServiceProvider\LaminasDiactorosServiceProvider');

$strategy = (new League\Route\Strategy\ApplicationStrategy)->setContainer($container);
$router   = (new League\Route\Router)->setStrategy($strategy);

$router->group('/API/v1', function (League\Route\RouteGroup $route)
{
  $route->map('GET', '/party/{id}',       'App\Controllers\PartyController::get');
  $route->map('GET', '/parties/{dos_nr}', 'App\Controllers\PartiesController::get');
});
´´´

And this is where it fails:

$response = $router->dispatch( $container->get('Laminas\Diactoros\ServerRequest'), $container->get('Laminas\Diactoros\Response') );



Any tips how I can debug this error?
philipobenito commented 3 years ago

I'd need to see the code related to the failing container definitions to help

philipobenito commented 3 years ago

Closing as stale