请告诉我,如何修复symfony错误:
无法自动连接的参数$request"App\Controller\ArticleController::create((":它引用类"Symfony\Component\BrowserKit\Request",但不存在此类服务。
控制器:
use AppEntityArticle;
use AppFormArticleType;
use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyComponentBrowserKitRequest;
use SymfonyComponentRoutingAnnotationRoute;
/**
* @Route("/article/create", name="creates_article")
*/
public function create(Request $request)
{
$article = new Article();
$form = $this->createForm(ArticleType::class, $article);
$form->handleRequest($request);
return $this->render('article/form.html.twig', [
'form' => $form->createView()
]);
}
日志:通道-请求
Matched route "creates_article".
{
"route": "creates_article",
"route_parameters": {
"_route": "creates_article",
"_controller": "App\Controller\ArticleController::create"
},
"request_uri": "http://symfony.loc/article/create",
"method": "GET"
}
我敢肯定,您使用了错误的Request
类命名空间。
应该是:
SymfonyComponentHttpFoundationRequest
而不是
SymfonyComponentBrowserKitRequest
所以试试:
use SymfonyComponentHttpFoundationRequest;
Symfony文档中的示例:https://symfony.com/doc/current/controller.html#the-请求对象即控制器自变量
use SymfonyComponentHttpFoundationRequest;
public function index(Request $request, $firstName, $lastName)
{
$page = $request->query->get('page', 1);
// ...
}