Jump to content

MVC Model


SchweppesAle

Recommended Posts

hi, just looking for some advice on how to choose appropriate controller names which adhere to the MVC pattern. 

 

I have this banner platform which I'm working on, and I felt that there where a bit too many controllers. 

 

Ex: I had one which allowed for modification of banners, one which created new banners, one which displayed a list of banners.  This was in addition to controllers which manage clients, locations, etc. 

 

I felt like there where too many controllers though so I deleted the modify, list, create controllers and placed all functionality within the "banner" controller (1 huge switch statement - see below). 

 

/*fetches the appropriate task so we know which function within the view to execute*/
      $task = JRequest::getVar('task', 'listBanners');
      $MV = parent::setMV();
      $model = $MV['model'];
      $view = $MV['view'];

      /*Fetches required Variables*/
      $id = JRequest::getInt('id', NULL);
      $Title = JRequest::getVar('Title', NULL);
      $bannerImage = JRequest::getVar('bannerImage', NULL);
      $destination = JRequest::getVar('destination', NULL);
      $locations = JRequest::getVar('locations', NULL);
      $startDate = JRequest::getVar('startDate', NULL);
      $endDate = JRequest::getVar('endDate', NULL);
      $state = JRequest::getVar('state', NULL);

      switch($task)
      {

        /*lists all available banners*/
        case 'listBanners':
            $Banners = $model->getBanners();
            $view->listBanners($Banners);
        break;

        /*use list to unpublish a banner*/
        case 'unpublish':
            $model->unpublish($id);
            header('Location: index.php?option=com_jtbanners&view=banners');
        break;

        /*use list to publish a banner*/
        case 'publish':
            $model->publish($id);
            header('Location: index.php?option=com_jtbanners&view=banners');
        break;

        /*displays options for each banner after clicking via list*/
        case 'clickBanner':
            $bannerImg = $model->getBannerImg($id);
            $view->bannerOptions($bannerImg);
        break;

         /*allows user to modify any of banner's settings*/
         case 'updateBanner':
            $bannerData = $model->getBannerData($id);
            $locations = $model->getLocations();
            $bannerImages = $model->getBannerImages();
            $view->updateBanner($bannerData, $locations, $bannerImages);
        break;

        /*saves changes user has made to a banner*/
        case 'update':
            $model->updateBanner($id, $Title, $bannerImage, $destination, $locations, $startDate, $endDate, $state);
            header('Location:index.php?option=com_jtbanners&view=banners');
        break;

        /*dispays form, allowing user to create a new banner*/
        case 'createBanner':
            $locations = $model->getLocations();
            $bannerImages = $model->getBannerImages();
            $view->createBanner($locations, $bannerImages);[sup][/sup]
        break;

        /*Inserts banner into database*/
        case 'insert':
            $model->newBanner($Title, $bannerImage, $destination, $locations, $startDate, $endDate, $state);
            header('Location: index.php?option=com_jtbanners&view=banners');
        break;

        default:
            $Banners = $model->getBanners();
            $view->listBanners($Banners);
        break;
      }
   }

 

In summary, is it best to name your controllers after verbs which describe their functionality or as in this case, nouns?

Link to comment
Share on other sites

We generally adhere to plurals (UsersController in combination with .htaccess you can create users/ignace)

 

Now back to MVC. You can create one controller for each page in your website or create one controller that handles multiple related actions:

 

class EditPageController extends Controller {
    public function __construct(..
}

//or

class PagesController extends Controller {
    public function addAction() {}
    public function editAction() {}
}

 

A good practice is to keep your controllers thin and your models fat. By this they mean that your controller should have nearly no logic while your models should have it all. This approach ensures software flexibility for example:

 

class UsersController extends Controller {
   public function addAction() {
      if (UserRepository::add($this->getRequest())) {
          $this->getResponse()->redirect('/users');
      }
   }
}

 

As you can see above my code knows nothing they don't know what sort of request they got for all they know I could have done something like:

 

> index.php --controller users --action add --username ignace --email ignace@mail.com

or 

POST http://domain.com/users/add
username ignace
email ignace@mail.com

or 

..

 

The best thing you can do is write your software in such a way so that everyone knows only what they need to know and nothing more the same goes for your website. Depending on what sort of request you get your software may return plain/JSON/XML/HTML/XHTML I list HTML and XHTML separatly because through content negotiation you can decide wether the client browser supports XHTML. The same goes for language an american gets everything in english while a frenchmen gets everything in french while your application logic remains mostly the same for all requests (http://www.w3.org/Protocols/rfc2616/rfc2616-sec12.html).

Link to comment
Share on other sites

I "normally" create one controller for each "type" of things then put different functions to handle the actions.  So

Users would have it's own controller.  Then inside the users controller there would be a function: login, logout, forgotpassword,

and everything else. Those deal with the login page, logout page and so forth.  This is generally how I set up my controller.

There are even times when there are a lot of misc things I need but there so small they may not need a controller

I use on called misc controller.

 

That's what I do. I use the same naming convention in Codeingitor, cakephp, zend, and the others.  Almost anything that deals in MVC, that's how I name them.

Link to comment
Share on other sites

*scratches head*

 

So does that mean that I added too much logic to my controllers? 

 

I figured that you use the controllers to assign appropriate models, make appropriate calls to the model then send that data over to the view. That's why each one has a switch statement. 

 

Should I rewrite this?  I'm trying to get it ready for an interview, that's why I'm kind of stressing the design right now

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.