Nov 27, 2010

A first controller action

The controller is modelized by a class that should be placed under business/{your-module}/controllers/. So in our case : business/front/controllers/. Just for a purpose of file lisibility, you can name your file Controller.php, you can name your file whatever you want, Simple MVC will find it :-) . We will create a FeedController that will handle the logic of
- adding feeds
- calling the feed urls and getting the feed items
- displaying the feed detail
So if we analyse those few lines, we will notice that we need some methods in our controller to :
- show the list page : this will be our index page, we will call it indexAction
- show the detail page : this will be our detail page, we will call it detailAction
- show an add page : we need an addAction
- save the added feed : we need a saveAction.
we will need to have few other method to do some glue.
So lets start!

class  FeedController extends CoreController
     * Inject dependencies
     * @param  Container  $container The Container
    public function setContainer(Container $container)
        // database is set on the business layer. this will allow the use of
        // the mvc without coupling automatically the database.
        $this->Database           = $container['Database'];
        $this->Request = $container['Request'];
        $this->languages   =  $container['Language'];

The first method we wrote is setContainer. this method allows us to customize what we need to have as accessible objects in our controller, it takes an instance of a Container, and gets some objects. In this case, we collect the Database object, since we need to store the feed addresses, the Request object since we need to deal with request variables, and the languages array since we need to manage many languages in our application.
Lets continue, the second method we will write is the alwaysAction, this action is actually always called before triggering our actions. Remember the call of frontDispatcher->route in the bootstrap file, this method will actually call the alwaysAction before calling our specific one (indexAction, addAction etc.). But why we need to write an always Action for our case? the response is that in all our actions, we will need to have an instance of the model we will use. for example, to list the feeds, we need this instance, to show the details, we will need it also etc. So instead of having to instanciate it in every action, we will override the alwaysAction and have our model instance in ther. the method will look like :

 public function alwaysAction()
        $this->model = new FeedModel($this->Database);

As you noticed, the model instance is also a class, and takes a database instance as a constructor parameter. We will look more in detail at the model in the following actions, and in the Understanding the Models section.
Next! we will write our index action, here is the code:

     * Index Action
     * will list the feeds
    public function indexAction()
        $feeds = $this->model->findAll();
        if (false !== $feeds)
            $this->view->feeds = $feeds->fetchAll();

That’s it? yes, this is all we need to have for the moment :-) what we are doing here is that: we get all our feed using the findAll method of the model, and fetch all of them in the view. We didn’t spoke about the feed yet, we will do that later on. all you need to understand now is that we are passing the feeds to the view, it is its responsibility to display them.
We need to make that first page work, what we are missing to get this working? the model definition, and the view definition. so lets see how to add our model definition.

Leave a comment