将应用程序移至Docker容器后,我的应用程序出现问题。我是Symfony的新手,但我不明白问题出在哪里。
我要调用UserController索引操作后,浏览器会抛出错误:
控制器“ App \ Controller \ UserController”具有必需的构造函数参数,并且在容器中不存在。您忘了定义这样的服务吗? 函数App \ Controller \ UserController :: __ construct()的参数太少,在第133行的/projekty/taskit/vendor/symfony/http-kernel/Controller/ControllerResolver.php中传递了0,并且恰好是1。
我的services.yaml文件:
parameters:
locale: 'en'
services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
public: false # Allows optimizing the container by removing unused services; this also means
# fetching services directly from the container via $container->get() won't work.
# The best practice is to be explicit about your dependencies anyway.
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
resource: '../src/*'
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
# controllers are imported separately to make sure services can be injected
# as action arguments even if you don't extend any base controller class
App\Controller\:
resource: '../src/Controller'
tags: ['controller.service_arguments']
# add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones
App\Entity\UserRepositoryInterface: '@App\DTO\UserAssembler'
UserController.php文件
<?php
namespace App\Controller;
use App\Service\UserService;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
/**
* Class UserController
* @package App\Controller
*/
class UserController extends AbstractController
{
/**
* @var UserService
*/
private $userService;
/**
* @param UserService $userService
*/
public function __construct(UserService $userService)
{
$this->userService = $userService;
}
/**
* @Route("/users/", name="users_index")
*/
public function index()
{
$users = $this->userService->findAll();
return $this->render('user/index.html.twig', array('users' => $users));
}
}
还有我的UserService.php文件代码
<?php
namespace App\Service;
use App\Entity\UserRepositoryInterface;
use Doctrine\ORM\EntityNotFoundException;
/**
* Class UserService
* @package App\Service
*/
class UserService
{
/**
* @var UserRepositoryInterface
*/
private $userRepository;
/**
* @param UserRepositoryInterface $userRepository
*/
public function __construct(UserRepositoryInterface $userRepository)
{
$this->userRepository = $userRepository;
}
/**
* @return array
* @throws EntityNotFoundException
*/
public function findAll() : array
{
$users = $this->userRepository->findAll();
if (is_null($users)) {
throw new EntityNotFoundException('Table users is empty');
}
return $users;
}
}
答案 0 :(得分:1)
在我看来,您正在做很多不必要的事情,它们已经是框架的一部分。像您的服务一样,它有什么作用?您可以直接从控制器调用存储库。在您的控制器中,您正在声明变量并进行构造,这也是不必要的。只需在控制器的函数中使用依赖项注入即可。这一点代码应该可以正常工作,并且可以替换控制器和服务(再次可以删除)。
<?php
namespace App\Controller;
use App\Repository\UserRepository;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
/**
* Class UserController
* @package App\Controller
*/
class UserController extends AbstractController
{
/**
* @Route("/users/", name="users_index")
*/
public function index(UserRepository $userRepository)
{
$users = $this->userRepository->findAll();
return $this->render('user/index.html.twig', array('users' => $users));
}
}