2017-10-10 73 views
0

我有一个文件夹ProjectFolder,其中包含子文件夹Mobile和模板indexMobile.html.twig使用注解在子文件夹中访问模板

AppBundle 
    | Resources 
    | views 
     | ProjectFolder 
     | Mobile 
      - indexMobile.html.twig 
     | someView.html.twig 
     | someView.html.twig 
     | someView.html.twig 

在我的控制器中我试着用各种recommandation与@Template doc@route doc

访问它,但是我不同的方法没有找到我已创建的模板,它一直要求我在views创建一个模板,而不是直接Mobile子文件夹。

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

-

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

-

class ProjectFolderController extends Controller 
    /** 
     * @Route("Mobile", name="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[]; 
    } 

但我得到这个错误:No route found for "GET/mobile/index"

回答

1

您提供在这个例子中,正确的解决方案:

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

默认使用的@Template找到模板的逻辑不支持子目录。这就是为什么你必须通过模板路径作为@Template的参数。

使用Twig命名空间应该很容易。例如:@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(和templates/mobile/index.html.twig在Symfony 4中)。

+0

感谢您的时间,解释为什么@Template不支持子目录。现在更清晰。 –

0

你在这里失踪的事情是,使用@Template像这样将默认使用此选项可搜索它:

“的appbundle:NAME(控制器):indexMobile.html.twig”

所以这个文件夹层次将工作:

AppBundle 
| Resources 
    | views 
     | Mobile 
     - indexMobile.html.twig 

使用MobileController这样的:

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

嗨,谢谢你的回答。是的,我得到了'@ Template'实际上默认搜索的提示。但实际上无法使用您的答案在子文件夹中进行搜索。我仍然无法找到如何使用'@ Template'(我编辑了我的问题) –

相关问题