当我尝试在构造函数中使用自动解析依赖项时,出现错误,尽管在方法上可以正常工作。
<?php
namespace App\Controller;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
class AuthorController
{
// Produce an error
// public $app;
// public $request;
// public $entityManager;
//
// public function __construct(Application $app, Request $request)
// {
// $this->app=$app;
// $this->request=$request;
// }
public function create(Application $app, Request $request)
{
}
}
Argument 1 passed to App\Controller\AuthorController::__construct() must be an instance of Silex\Application, none given
答案 0 :(得分:1)
定义路由时,必须将$app
作为参数传递给构造函数
$app->post(
'/author',
function (Request $request) use ($app) {
$controller = new AuthorController(
$app,
$request
);
return $controller->create();
}
);