问题描述
我正在重写一个PHP应用程序,该应用程序在过去十年中得到了发展,并选择了League \ Route,League \ Container和Aura \ sql来建立坚实的基础。 我在将路由器中的所有组件连接在一起时遇到了麻烦-其他组件似乎可以正常工作:
索引:
$container = new League\Container\Container;
$container->defaultToShared(); // Added along the line - not sure whether this is needed
$container->delegate( new League\Container\ReflectionContainer ); // Enable auto wiring
$container
->add('dbpro',Aura\sql\ExtendedPdo::class)
->addArgument('pgsql:dbname=app client_encoding=UTF8')
->addArgument('app')
->addArgument(NULL)
->addArgument($db_attributes);
$container->add('modl_party',App\Model\Party::class)
->addArgument('dbpro');
$container->add('ctrl_party',App\Controllers\Party::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\Party::get');
$route->map('GET','/parties/{dos_nr}','App\Controllers\Parties::get');
});
try {
$response = $router->dispatch(
$container->get('Laminas\Diactoros\ServerRequest'),$container->get('Laminas\Diactoros\Response')
);
} catch (League\Route\Http\Exception\NotFoundException $e) {
$response = $container->get('Laminas\Diactoros\Response');
$response->getBody()->write($e->getMessage());
$response = $response->withStatus($e->getStatusCode());
}
$container->get('Laminas\Diactoros\Response\SapiEmitter')->emit($response);
控制器:
namespace App\Controllers;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Laminas\Diactoros\Response;
class Party {
protected $party;
public function __construct( \App\Model\Party $party )
{
$this->party = $party;
}
public function get(ServerRequestInterface $request,array $args) : ResponseInterface
{
$party = $this->party->get( $args['id'] );
$body = json_encode( $party );
$response = new Response;
$response->getBody()->write($body);
return $response
->withAddedHeader('content-type','application/json')
->withStatus(200);
}
}
型号:
namespace App\Model;
use \Aura\sql\ExtendedPdo;
class Party {
protected $pdo;
protected $table;
public $id;
public function __construct( \Aura\sql\ExtendedPdo $pdo ) {
$this->pdo = $pdo;
$this->table = 'parties';
}
public function get( $id )
{
$sql = "SELECT ...";
$binds = array( 'id' => $id );
$obj = $this->pdo->fetchObject( $sql,$binds,'App\Model\Party',[$this->pdo] );
return $obj;
}
}
结果:
$ curl <URI>/API/v1/party/15285
<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->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->reflectArguments(Object(ReflectionMethod),Array)
#3 [...]/vendor/league/container/src/Container.PHP(183): League\Container\ReflectionContainer->get('Aura\\sql\\Extend...')
#4 [...]/vendor/league/container/src/Argument/ArgumentResolverTrait.PHP(45): League\Container\Container->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>
其他信息:
- 我可以从容器中成功获取ExtendedPdo实例
- 我可以从容器和':: get(15285)'中成功获取App \ Model \ Party实例
- 我可以从容器和':: get(15285)'中成功获取App \ Controllers \ Party实例
- 我也使用文档中提供的示例尝试了JsonStrategy,但是得到了相同的结果
- 我未包含LaminasDiactorosServiceProvider.PHP代码,但这与文档中的示例相同(除了将“ Zend”替换为“ Laminas”之外)
->我认为错误一定是路由器“构建”响应方式的某个地方?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)