0
votes

I'm relatively new to Symfony 2 and I'm working on an application which has an admin only login area located under domain.com/admin/home/ (which itself is a separate bundle called AdminBundle) allowing the admin to manage products

However the same application is also going to have a BlogBundle which will require an admin area to manage blog posts.

What is the best way to tackle this, is there a best practice for multiple admin areas?

It makes sense to keep the blog admin section within the BlogBundle for better re-usability/portability, however all of the login settings will be in the AdminBundle which seems wrong.

1
Check out one of the premade admin solutions, such as SonataAdmin(github.com/sonata-project/SonataAdminBundle) or Symfony2admingenerator(This is what I prefer -- github.com/symfony2admingenerator/AdmingeneratorGeneratorBundle).Mike

1 Answers

1
votes

One way to do this is to use your security.yml file to secure a set of routes. For example;

access_control:
   ...
   - { path: ^/admin, roles: [ROLE_ADMIN] }

Then when you create your actions you simply need to give them a route with an admin prefix to secure them against all users other than admin. My examples use annotation for routing but you can do the same with yml.

/**
 * Edit a Blog entity.
 *
 * @Route("/admin/blog/{id}/edit", name="blog_edit")
 * @Template()
 */
public function editAction()
{
...

Or you could secure every action in the controller by prefixing the class:

/**
 * AdminBlog controller.
 *
 * @Route("/admin/blog")
 */
class AdminBlogController extends Controller
{
...

Then create another controller which handles displaying the blog posts to the end user and give them unsecured routes.

If you want to secure other areas in seperate bundles your can do it in the same way by have secure and unsecure controllers/ methods.actions