我想从Controller类SYmfony4中的函数获取public
目录
我尝试了三种类型,但徒劳无功。
print $this->get('kernel')->getProjectDir();
print $container->getParameter('kernel.project_dir');
print $this->appKernel->getProjectDir();
第一个显示这样的错误消息, 我想有一些提示.....
Service "kernel" not found: even though it exists in the app's container, the container inside "App\Controller\MemberController" is a smaller service locator that only knows about the "doctrine", "form.factory", "http_kernel", "parameter_bag", "request_stack", "router", "security.authorization_checker", "security.csrf.token_manager", "security.token_storage", "serializer", "session" and "twig" services. Unless you need extra laziness, try using dependency injection instead. Otherwise, you need to declare it using "MemberController::getSubscribedServices()".
答案 0 :(得分:2)
你很近。在您的控制器方法中尝试AbstractController::getParameter()
:
$this->getParameter('kernel.project_dir') . '/public';
仅供参考,这实际上不是一个好习惯,并且会增加测试的复杂性。 更好的解决方案是使用依赖注入。