Jump to content

MVC Design Pattern - Building a better controller


SchweppesAle

Recommended Posts

Hi, was just looking for some input regarding how to properly set up a controller.  My usual approach is through the use of switches and multiple controllers(per view) to keep it from becoming too big and confusing. 

 

I've been told that it would better to avoid switch statements all together and use a more dynamic controller though. 

 

Here's one of my larger controllers:

 

<?php
defined('_JEXEC') or die ('restricted access');
jimport('joomla.application.component.controller');
require_once('master.php');

class JtbannersControllerBanners extends JtbannersControllerMaster
{

function __construct()
   {
       parent::__construct();
   }

   function display ()
   {
      /*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);
      $bannerSize = JRequest::getInt('bannerSize', 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);
      $client = JRequest::getInt('client', NULL);
      /*JREQUEST_ALLHTML is bugged
      $customCode = JRequest::getVar('customCode', NULL, 'post', 'STRING', JREQUEST_ALLOWHTML);
      */
      $customCode = $_POST['customCode'];
      $selected = JRequest::getVar( 'selected' , array() , '' , 'array' );

      switch($task)
      {

        /*lists all available banners*/
        case 'listBanners':
            $Banners = $model->getBannerList();
            $view->listBanners($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();
            $bannerSizes = $model->getAllSizes();
            $clients = $model->getAllClients();

            $view->modifyBanner($bannerData, $locations, $bannerSizes, $bannerImages, $clients);
        break;

        /*allows user to create a new banner*/
        case 'createBanner':
            $locations = $model->getLocations();
            $bannerImages = $model->getBannerImages();
            $bannerSizes = $model->getAllSizes();
            $clients = $model->getAllClients();

            $view->createBanner($locations, $bannerImages, $bannerSizes, $clients);
        break;

        /*display data collected from users "onclick"*/
        case 'bannerStatistics':
            $id = JRequest::getInt('id', NULL);

            /*returns all click data associated with specific bannerid*/
            $bannerClickData  = $model->bannerClickData($id);

            /*runs above data through a series of bot filters and returns a valid numerical value*/
            $realClicks = $model->getRealClicks($bannerClickData, $id);

            /*returns all other data associated with banner,img location, etc*/
            $bannerData = $model->getBannerData($id);
            $bannerData['clicks'] = $realClicks;

            /*resizes banner for our purposes*/
            $newSize = $model->resize($bannerData['img_location']);
            $bannerData['height']= $newSize['height'];
            $bannerData['width'] = $newSize['width'];
            
            $view->bannerStatistics($bannerClickData, $bannerData);
        break;

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

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

        /*delete banner entries*/
        case 'delete':
            $model->delete($selected);
            header('Location:index.php?option=com_jtbanners&view=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;

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

   

}


?>

 

 

So...how could I build a better mousetrap  :confused:

 

Link to comment
Share on other sites

click

y

next go to:

PHP OOP

then go amazon or from where you get your books and get a book for OOP Principles or google about them and learn

 

i haven't read much from that code because it's big and nasty but for each case should be a function(Method) then when it's parsed to the view depending on what user wants that method will be called you don't just throw in there all those cases and variables.

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.