Symfony2: best practive how to get data from another bundle?

531 views Asked by At

I'm new to symfony2 and there are some things I don't get right now. In this case I want to display all albums made by a band or artist. If the user is logged in I want to display on the same page, if one album is in the collection of this user or not.

I've different bundles like "ArtistBundle", "UserBundle", "AlbumBundle" etc. Since the user related information is nothing which really belongs in the ArtistBundle ("separation of concerns") I want to get the information from the UserBundle.

Right now I see only two ways to do this:

<?php
namespace Acme/ArtistBundle/Controller;
use Acme/UserBundle/User

public function indexAction()
{
    ...
    $user = new User($userId);
    $collectionInfo = $user->checkArtistAlbums($artistId);
    ...
}

This solution might work but it makes unit tests harder since i'm not using dependency injection (which would be the correct way i think).

The other possibilty would be embedding the UserBundle controller in the template. Which can't be done since I need the information directly in the output of the ArtistBundle.

So what would be the "best practice" here? Defining a UserBundle Service? Somehow configure the ArtistBundle that an object of UserBundle is set in the construction of the object?

2

There are 2 answers

1
TroodoN-Mike On

In my opinion I would create myProjectBundle. Then you create an Entity folder and there you create Album Artist and User folders. Inside you create Doctrine for each tables (album, atrtis, user).

What I would do is call service (create service folder and service class) in my controller (loginAction) that would pull information required (get all albums and artist for current user). Then use template to draw the login page and use another template to display rest of the information.

0
hakre On

The separation of concerns is to have different bundles for different things. You controller can't separate, it needs to bring the different things together, to make the play.

But the bundles are still separated. So even if you think it's not separated because you bring things together in your controller action, the opposite is the case.

You controller helps you to separate things while bringing them together but only using separated bundles.