pinch boi triggered af
pinch boi triggered af

Reputation: 544

Symfony2 Routing within a bundle

I have a symfony project where i have seperated my code into two different bundles lets say HomeBundle and AppBundle however the routes are commonly accessible inside both bundle.

eg:- /home defined in HomeBundle can also be accessed from AppBundle

and

/App defined in AppBundle can also be accessed from HomeBundle

what i want is to access /home ONLY from HomeBundle and /App from Only AppBundle I'm looking for 'structuring' my code in a way where i dont have to write any logic to accomplish this, but instead leverage symfony frameworks structure to do this for me, something along the lines of restricting routing to only within the bundle, or some way where the route definition goes 'out of scope' when inside the wrong bundle

im using annotaions for routing, and defined in app/config/routing.yml

edit: the application requires subomain partitions so i'm also using this host/default with placeholder

any help would be appreciated :) thanks

Upvotes: 2

Views: 497

Answers (3)

pinch boi triggered af
pinch boi triggered af

Reputation: 544

I was under the assumption that business logic inside bundles are isolated and would not be 'aware' of routes defined inside other bundles, turns out the routes are globally available to all bundles, i wanted to use the framework of symfony to achieve what i asked for, it happens so there is no way to do it. i probably should use Listeners or before filters etc to achieve this. John Noel answered this.

Luckily for me, the bundles happen to operate on separate Domains (one on the main domain and the other on the subdomain) i noticed this after a chat with @John Noel he suggested matching the Route Based on the Host/Domain. i had this in place previously, upon a second look i figured out i have to -based on the domain redirect all (except for a white list of routes) routes to a controller that throws a 404 exception. Here is what i mean.

Home_restrict:
    path:     /{slug}
    host:     "{domain}"
    defaults:
        _controller: HomeBundle:Home:notfound
    requirements:
       domain: example.com|www.example.com
       slug: ^(?!.*(admin|login)$).*

Routes that appear on top of the list on the page takes precedence, so this should be placed appropriately

Upvotes: 1

user4545769
user4545769

Reputation:

Unfortunately this isn't going to be possible, primarily because of the architecture of Symfony bundles. While separating your code into bundles is good from a cognitive point of view, Symfony doesn't have the concept of being "in" a bundle; what code is run (specifically your controller actions) is usually handled by routes, and routes have to be compiled together from ALL bundles to ensure that a request can be routed successfully.

You would have to write extra code to accomplish what you're asking - my initial thought would be your own Controller class (maybe extending from FrameworkBundle\Controller\Controller) that ran some code before a request (see documentation that may achieve this).

Fundamentally this is an architectural decision for you and my question would be - why do you not want to be able to access /home from the AppBundle? Would a security / role set up work as well or are you trying to sandbox code?

EDIT After some further detail, it turns out that the requirement is for two different actions for the same URL path (e.g. /home) but on different domains e.g. www.example.com/home and subdomain.example.com/home. In this instance it wasn't about code sharing but URLs so Symfony's host/domain based routing solution worked.

Upvotes: 2

Rvanlaak
Rvanlaak

Reputation: 3085

You can add a routing.yml per bundle, and include these in your "global" routing:

# src/HomeBundle/Resources/config/routing.yml
home:
    resource: "@HomeBundle/Controller/HomeController.php"
    type:     annotation

and

# src/AppBundle/Resources/config/routing.yml
home:
    resource: "@AppBundle/Controller/AppController.php"
    type:     annotation

then import them in app/config/routing.yml, and maybe even prefix it over there?

# app/config/routing.yml
app:
    resource: "@AppBundle/Resources/config/routing.yml"
    prefix:   /app

Upvotes: 0

Related Questions