I would like to ask, what is the best way to structure a project with frontend and backend in Symfony2? In other versions of Symfony this is easy to achieve, because you can create two applications - frontend and backend - then all the libraries/models will become shared between those applications.
Now in Symfony2, everything is a bundle. What is not really clear for me is how could I represent the "two" applications, frontend and backend. Should I create two namespaces - frontend and backend? I would like to keep my entities in just one place, rather than accessing them from the two applications.
Just have one application, create an AdminBundle
or BackendBundle
or whatever for your project that has all the tools you'll need in the back end, and use firewalls and access control lists in security.yml
to create separate routes for the two.
No need to create separate app directories, mess with the console script, or anything like that. By default, Symfony knows how to find entities and other resources for any bundle that you have registered, so you'll be able to share them easily.
This documentation entry on Security is a good place to start learning about access control and firewalls. Here's a quick sample of what my security config looks like:
firewalls:
main:
pattern: /.*
anonymous: true
form_login: true
access_control:
- { path: /admin/.*, roles: ROLE_ADMIN }
- { path: /.*, roles: IS_AUTHENTICATED_ANONYMOUSLY }
If you want complete authentication separation for / and /admin, create a new admin firewall: firewalls do not share authentication information between each other, so you can track users and their sessions separately if you'd like.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With