Homunculus Reticulli
Homunculus Reticulli

Reputation: 68476

Symfony2 bundles confusion

I am following the instructions on creating a sample application. I am trying to quickly get to grips on how to create a few (for now) static pages - no user authentication, no ORM etc, just to get something up and running.

I have created two bundles:

$ php app/console generate:bundle --namespace=app1/FooBundle --format=yml
$ php app/console generate:bundle --namespace=app2/FooBarBundle --format=yml

from the documentation (and knowledge of how Symfony 1.x worked), I expected to be able to access the two 'applications' using:

http://sf2.examples.localhost/app_dev.php/app1/hello/angrycoder
http://sf2.examples.localhost/app_dev.php/app2/hello/angrycoder

However, in both cases, I simply got a 404 error.

Upon going back to the documentation, I read that the correct URL to use was;

http://sf2.examples.localhost/app_dev.php/hello/angrycoder

That seems to 'work' in so far as a page with angrycoder was displayed.

HOWEVER - I have no idea which application has actually been called - since I haven't yet figured out how to display the debug toolbar.

My question therefore is this:

  1. How do I get to a specific page (static or otherwise) in a given bundle?.

[[Edit]]

This is what my app/config/routing.yml file looks like:

app1CoreBundle:
    resource: "@app1CoreBundle/Resources/config/routing.yml"
    prefix:   /

app2CoreBundle:
    resource: "@app2CoreBundle/Resources/config/routing.yml"
    prefix:   /

# Internal routing configuration to handle ESI
#_internal:
#   resource: "@FrameworkBundle/Resources/config/routing/internal.xml"
#   prefix:   /_internal

As can be seen, both have a prefix of '/'. So when I type:

http://sf2.examples.localhost/app_dev.php/hello/angrycoder

Which application is actually called (and why?).

From my understanding, the only (obvious way) to be able to call specific applications - is to use different prefixes for different applications - or am I missing something?

Upvotes: 1

Views: 721

Answers (2)

Peter Bailey
Peter Bailey

Reputation: 105916

Forget what you know about Symfony 1.x when you're working with Symfony 2. Besides the name and the overall MVC approach, they are very different frameworks.

Symfony 2 doesn't have "apps" the way Symfony 1 does. You can sort of treat your own Bundles like symfony 1 apps, but make no mistake: there is no direct analogue. Bundles are much more generic and flexible than apps (which is a good thing)

As you're discovering, access to a bundle's controllers is completely governed by the routing system.

Upvotes: 1

DonCallisto
DonCallisto

Reputation: 29932

You have to use the routing system for that kind of needs.
So if you go to app/config/routing or, more in detail, into src/app1/FooBundle/Resources/config/routing.yml you can change the page displayed

In detail

// app/config/routing.yml
AcmeHelloBundle
resource: "@AcmeHelloBundle/Resources/config/routing.yml"
prefix:   /
//end

// src/Acme/AcmeHelloBundle/Resources/config/routing.yml
AcmeHelloBundle_homepage:
pattern:  /hello/{name}
defaults: { _controller: AcmeHelloBundle:Default:index }
//end

In that way, i'am telling to symfony to find che routing system and rules, for AcmeHelloBundle into the file AcmeHelloBundle/Resources/config/routing.yml
After, into AcmeHelloBundle/Resources/config/routing.yml I say that every path that will come with a url like /hello{placeHolder} will pass into Default controllor and will render index.

Hope it is clear.

Upvotes: 2

Related Questions