Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Symfony 访问带有批注的子文件夹中的模板_Symfony_Routing_Annotations - Fatal编程技术网

Symfony 访问带有批注的子文件夹中的模板

Symfony 访问带有批注的子文件夹中的模板,symfony,routing,annotations,Symfony,Routing,Annotations,我有一个文件夹ProjectFolder,里面有一个子文件夹Mobile和一个模板indexMobile.html.twig AppBundle | Resources | views | ProjectFolder | Mobile - indexMobile.html.twig | someView.html.twig | someView.html.twig | someView.h

我有一个文件夹
ProjectFolder
,里面有一个子文件夹
Mobile
和一个模板
indexMobile.html.twig

AppBundle
  | Resources
    | views
      | ProjectFolder
        | Mobile
          - indexMobile.html.twig
        | someView.html.twig
        | someView.html.twig
        | someView.html.twig
在我的控制器中,我尝试使用和访问它

但是我的不同方法没有找到我已经创建的模板,它一直要求我直接在
视图
中创建模板,而不是在
移动
子文件夹中创建模板

class ProjectFolderController extends Controller
    /**
         * @Route("@Mobile/indexMobile")
         * @Method({"GET", "POST"})
         * @Template
         */
        public function indexMobileAction()
        {
            return[];
        }
-

-

实际上这是可行的,但这不是我应该使用的方式:

class ProjectFolderController extends Controller
    /**
         * @Route("/mobile/index")
         * @Method({"GET", "POST"})
         * @Template("@ProjectFolder/Mobile/index.html.twig")
         */
        public function indexMobileAction()
        {
            return[];
        }
编辑

经过几次尝试,我发现:

class ProjectFolderController extends Controller
/**
     * @Route("@ProjectFolder/Mobile", name="/mobile/index")
     * @Method({"GET", "POST"})
     * @Template
     */
    public function indexMobileAction()
    {
        return[];
    }

但是我发现了这个错误:
没有为“get/mobile/index”找到路由

这里缺少的是,默认情况下,使用@Template这样的方法会使用它来搜索它:

“AppBundle:NAME(Controller):indexMobile.html.twig”

因此,此文件夹层次结构将起作用:

AppBundle
 | Resources
   | views
       | Mobile
         - indexMobile.html.twig
像这样使用MobileController:

class MobileController extends Controller
{
    /**
     * @Route("Mobile/indexMobile")
     * @Template
     */
    public function indexMobileAction()
    {
        return [];
    }
}

在本例中,您提供了正确的解决方案:

class ProjectFolderController extends Controller
{
    /**
     * @Route("/mobile/index")
     * @Method({"GET", "POST"})
     * @Template("@ProjectFolder/Mobile/index.html.twig")
     */
    public function indexMobileAction()
    {
        return[];
    }
}
默认情况下,
@Template
用于查找模板的逻辑不支持子目录。这就是为什么必须将模板路径作为
@template
的参数传递


使用细枝名称空间应该很容易。示例:
@Template(“@App/Mobile/index.html.twig”)
查找
src/AppBundle/Resources/views/index.html.twig
,而
@Template(“Mobile/index.html.twig”)
将查找
App/Resources/views/Mobile/index.html.twig
(以及
在Symfony 4中的
模板/Mobile/index.html.twig
@Template
实际上是默认搜索。但实际上无法使用您的答案在子文件夹中搜索。我仍然找不到如何将它与
@Template
(我编辑了我的问题)感谢您花时间解释@Template不支持子目录的原因。现在更清楚了。
class MobileController extends Controller
{
    /**
     * @Route("Mobile/indexMobile")
     * @Template
     */
    public function indexMobileAction()
    {
        return [];
    }
}
class ProjectFolderController extends Controller
{
    /**
     * @Route("/mobile/index")
     * @Method({"GET", "POST"})
     * @Template("@ProjectFolder/Mobile/index.html.twig")
     */
    public function indexMobileAction()
    {
        return[];
    }
}