18
votes

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.

4

4 Answers

12
votes

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.

6
votes

I have tried the approach suggested by PAStheLoD (creating app_backend and web_backend, etc) but I found it too much hassle; you have to remember which app to target from console, maintain separate bootstrap files, and the directory structure gets messy.

The method recommended by the Symfony documentation Symfony documentation is to have a separate project for each app. You can then create a separate area for your source code, write most (if not all) of your bundles there and then simply adjust how they are included in autoload.php of both apps.

e.g.

admin_app/
    app/
    bin/
    src/
    web/
frontend_app/
    app/
    bin/
    src/
    web/
common_src/
    Acme/
        AdminBundle/
        DataBundle/
        jQueryBundle/

Then in *_app/app/autoload.php:

$loader->registerNamespaces(array(
    'Acme' => __DIR__ . '/../../common_src'
));
2
votes

As far as I know you still can do the more or less the same thing with Symfony2. Just rename app/ to app_frontend and make a copy to app_backend, also duplicate web/ the same way. Then everything else can live in bundles. Bundles are very powerful, because they can contain routes, configuration or anything else, so you can share what you want and perfectly isolate what you don't want to share.

There might be some problems with the bin/ scripts due to renamed directories, but you just have to correctly configure them (or raise it as an issue at Symfony's Github site.)

2
votes

How about having one app and creating three bundles, src/Vendor/BackendBundle,src/Vendor/FrontendBundle and src/Vendor/SharedBundle. SharedBundle holds the entities and other shared parts betweeen the FrontendBundle and BackendBundle