sonam
sonam

Reputation: 3760

structure application in symfony 2

I am building a online store with symfony 2.1. Basically I have frontend section from where users of my site can view, add items and backend section for admin users to manage the entire site. Frontend and backend section will have different template but the database models (Doctrine) are same for both.

How do i structure my application?

Previously I was using symfony 1.4 where I could just create frontend apps and backend apps. Symfony 2 deals with bundles. Should I create 2 bundles one for frontend and one for backend? Creating such structure, how will be able to share models between them.

Please suggest me some structure for my application.

Upvotes: 2

Views: 1235

Answers (3)

Cerad
Cerad

Reputation: 48865

As the previous answers show, there is no single best approach. I find it useful to have three bundles. A CoreBundle containing the entities along with other shared functionality and then Frontend/Backend bundles sitting on top of the core bundle. But again, there is no single best approach.

Upvotes: 1

Elnur Abdurrakhimov
Elnur Abdurrakhimov

Reputation: 44841

I suggest you going without bundles and using subnamespaces for the backend stuff:

  • Vendor\Controller\UserController — the frontend user controller,
  • Vendor\Controller\Backend\UserController — the backend user controller.

Templates will be subnamespaced also:

  • app/Resources/views/User/view.html.twig,
  • app/Resources/views/Backend/User/view.html.twig.

You can refer to subnamespaced templates from other templates as:

{% include ':Backend\User:someTemplate.html.twig' %}

Upvotes: 3

moonwave99
moonwave99

Reputation: 22797

As symfony2 relies on namespaces, you can share classes between them easily.

Let's say you defined your entities in FrontendBundle, they will be like:

namespace Acme\FrontendBundle\Entity;

/** @ORM annotations stuff */
class MyEntity
{

    protected $id;

    ...

}

You can then refer to them either by creating from scratch:

$newEntity = new \Acme\FrontendBundle\Entity\MyEntity();

or by importing them via use:

namespace Acme\BackendBundle\Controller;

use Acme\FrontendBundle\Entity\MyEntity;

class AdminController extends Controller
{

    public someAction()
    { 

        $newEntity = new MyEntity();

    }

}

Doctrine repositories use a slightly different notation:

$this-> getDoctrine()
    -> getEntityManager()
    -> getRepository('FrontendBundle:MyEntity')
    -> findAll();

being it NameBundle:EntityName.

Refer to symfony best practices to get a better clue!

Upvotes: 2

Related Questions