4

I'm developing a bundle with frontend and backend. I follow instructions about the best way to structure controllers and views for backend and frontend parts here and here. But I can't find how to specify subdirectories in my routing configuration file. I try to put this, but it does not work.

post:
  pattern:  /
  defaults: { _controller: "HavactBlogBundle:Backend/Post:Backend/index" }
Community
  • 1
  • 1
smoreno
  • 3,129
  • 3
  • 32
  • 49

4 Answers4

16

try this: replace the slash with the backslash

post:
    pattern:  /
    defaults: { _controller: "HavactBlogBundle:Backend\Post:index" }
Lhassan Baazzi
  • 1,128
  • 1
  • 11
  • 21
  • The Backslash scape next character "P" and result this error: Unable to find controller "HavactBlogBundle:Backend
ost". I resolved with bouble backslash and works for controller, but not for template. index is in backend subdirectory, what i need is something like this HavactBlogBundle:Backend\\Post:Backend/Post/index – smoreno Mar 19 '12 at 04:08
  • 1
    when u want to render the template in action index use this: `return $this->render('HavactBlogBundle:Backend:Post:index.html.twig');` – Lhassan Baazzi Mar 19 '12 at 09:02
  • Yes, this works to render templates inside an action, but not in the routing config file. defaults: { _controller: "HavactBlogBundle:Backend:Post:index.html.twig" } – smoreno Mar 19 '12 at 13:12
  • I believe it is "doable" by making it like this: { _controller: "HavactBlogBundle:Backend/Post:index.html.twig" } the convention with semicolons says something like "bundle:dir:file", where Your dir is "Backend/Post". – thorinkor Dec 05 '13 at 13:53
5

For those people not wanting to expose their controllers as a service (which is an indirect solution to the problem), you specify the route as such.

route_name: path: /path defaults: { _controller: BundleName:Namespace/Controller:action }

Namespace is your subdirectory in the bundle's Controller directory followed by / to separate it.

All else should work just the same.

John Pancoast
  • 1,241
  • 16
  • 14
  • Works for me in Symfony 2.7. Slash for separation is fine - it's just important to remember correcting the namespace of those controllers moved to a subdirectory. – Atan Jun 21 '16 at 11:29
2

I relsoved exposing my controller as service

post:
pattern:  /
defaults: { _controller: "my.controller.service.id:indexAction" }
smoreno
  • 3,129
  • 3
  • 32
  • 49
0

In routing YAML:

defaults: { _controller: Org\FancyBundle\Controller\Page\Blog\CommentsController::fancyAction }

The difference here is that I don't use quotes around the string and YAML is okay with that. In a Twig template:

{% render "Org\\FancyBundle\\Controller\\Page\\Blog\\CommentsController::listAction" with {} %}

I've never had any problems with escaping that I know of. Symfony 2.0.9, PHP 5.3.9 on Windows/IIS (sigh)

OrganicPanda
  • 2,667
  • 1
  • 27
  • 28