【发布时间】:2019-06-18 17:02:28
【问题描述】:
在 Symfony 3.4 中,我有一个控制器,它有一些从 URL 填充的参数,而且我需要注入一个服务。我收到以下错误消息:
控制器 “Acs\EventNodeBundle\Controller\MainController::calendarAction()” 要求您为“$router”参数提供一个值。任何一个 该参数是可空的,并且没有提供空值,没有 已提供默认值或因为存在非可选 这之后的争论。
为什么 Symfony 不注入路由器?这是我的代码:
控制器:
...
use Symfony\Bundle\FrameworkBundle\Routing\Router;
/**
* Class MainController
*/
class MainController extends Controller
{
/**
* @param Router $router
* @param null|int $year
* @param null|int $month
* @return RedirectResponse|Response
*/
public function calendarAction(Router $router, $year = null, $month = null)
{
...
services.yml:
...
services:
# default configuration for services in *this* file
_defaults:
# automatically injects dependencies in services
autowire: true
# automatically registers services as commands, event subscribers, etc.
autoconfigure: true
# services cannot be fetched directly from the container via $container->get()
# need to override this setting individually
public: false
bind:
$entityManager: '@Doctrine\ORM\EntityManagerInterface'
# enable autowiring for controllers
Acs\EventNodeBundle\Controller\:
resource: '../../Controller'
public: true
tags: ['controller.service_arguments']
...
还有路由:
...
eventnode_main:
path: /{year}/{month}
methods: [GET]
defaults: { _controller: AcsEventNodeBundle:Main:calendar, year: null, month: null}
requirements:
year: '\d+'
month: '\d+'
...
【问题讨论】:
标签: symfony controller