• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I improve my controller???

#1
[eluser]Unknown[/eluser]
Hello, so I've been reading more and more about MVC. I often see that it's recommended to keep your controllers skinny...

Basically I just use my controllers to pass data to the appropriate view, but that tends to take up a ton of code( I couldn't even fit the whole controller in 1 post!)... I've included a less detailed version of my controller to see if I can receive any help with ways to improve my codeigniter coding...


NOTE: there may be some errors in consistency of names within code cause I changed a lot of names from original code just to feel safer about displaying my code..

ANY comments, recommendations are appreciated!! thanks for even taking time to look at this post! appreciate it!

Code:
<?php
Class Location extends CI_Controller
{
public function index()
{
$this->input->set_cookie('location','1','28800');  // set proper cookies
$this->load->model('Mcategories'); // load necessary model
$data['cats']  = $this->Mcategories->getAllCats(); // stuff array using an sql query inside model
$data['subcats'] = $this->Mcategories->GetCatsAndSubCats();  // stuff array using an sql query inside model
$data['title']  = "Location - California";  // assign title for page, to be sent to view
$data['keywords'] = "california, beaches,  hot weather, CTI"; // assigns meta data, to be sent to view
$data['urlname'] = "stonybrook"; // assigns more data that i use inside view
$data['locationname'] = "California location"; // assigns more data
$data['region']         = "Southern";
$data['colid']  = '1';
$data['directoryyes'] = TRUE;
$this->load->view('location_home', $data);  // send all above data to appropriate view
}

        // bellow is controller info for when url is: www.example.com/location/view

public function view($cat = 0,$subcat = 0,$idnum = 0) // loads most general 'functions' for view mode
{
$this->input->set_cookie('location','1','28800'); // set cookie
$this->load->model('Mcategories'); // loads model for category tables
$this->load->model('Msubcategories'); // loads model for subcategories
$this->load->model('Mgetads'); // loads model for viewing ads
$data['cats']  = $this->Mcategories->getAllCats(); // calls function to put all categories in an array
$data['catname'] = $this->Mcategories->getCatName($this->security->xss_clean($cat)); //returns category name based on $cat in URL
$data['catid']  = $this->Mcategories->getCatId($this->security->xss_clean($cat)); // returns category id (numerical value)
$data['caturl']  = $this->Msubcategories->GetCatUrlforSubCat($data['catid']); // gets the caturl for each subcat
$data['subcats'] = $this->Msubcategories->getSubCats($data['catid']); // puts subcategories (of whatever category is in the URL) into an array
$data['subcatid'] = $this->Msubcategories->GetSubCatId($this->security->xss_clean($subcat),$data['catid']); // gets subcatid, if there is none, it returns '0'
$data['keywords'] = "california, blah blah blah"; //assigning data to be sent to view
$data['title']  = "California title goes here"; // page title
$data['urlname'] = "cali"; // root URL for this class(used to make links)
$data['locationname'] = "California baby"; // assigning more data to be sent to view
$data['region'] = "Southern"; // region
$data['colid']  = '1';
$data['adnum']  = $this->security->xss_clean($idnum); //grabs id# from 5th URI segment, cleans it up and assigns it to data array

if ($cat == '0') { // no category chosen, shows home page for location
$data['directoryyes'] = TRUE;
$this->load->view('location_home', $data);
}
elseif ($cat != '0' && $subcat == '0') // viewing ads within category
{  
  $this->input->set_cookie('categid', $data['catid'],'0'); // sets cookie for category id
  $this->input->set_cookie('subcategid', $data['subcatid'], '0'); // sets subcategory id cookie
  $data['title']  = $data['catname']; // page title
  $data['catads']  = $this->Mgetads->getAdsForCat($data['colid'], $data['catid'], $data['caturl']); // grabs ads based on current category & location
  
  $this->load->view('location_view', $data);  
}
elseif ($subcat !='0' && $idnum == '0') // viewing ads within subcategory
{
  $this->input->set_cookie('categid', $data['catid'],'0'); // sets cookie for category id
  $data['subcaturl'] = $this->Msubcategories->GetSubCatUrl($this->security->xss_clean($subcat));
  $data['subcatname'] = $this->Msubcategories->getSubCatName($this->security->xss_clean($subcat));
  $this->input->set_cookie('subcategid', $data['subcatid'], '0'); // sets subcategory id cookie
  $data['subcatads'] = $this->Mgetads->getAdsForSubCat($data['colid'], $data['catid'], $data['caturl'], $data['subcatid']); // grabs ads based on current subcat and location
  $data['title']  = $data['subcatname']; // page title
  $this->load->view('location_view', $data);  
}
elseif ($idnum !='0')   // must call function to display corresponding ad
{  // general functions that need to be called no matter what ad
  $data['subcaturl'] = $this->Msubcategories->GetSubCatUrl($this->security->xss_clean($subcat));
  $this->input->set_cookie('categid', $data['catid'],'0'); // sets cookie for category id
  $data['subcatname'] = $this->Msubcategories->getSubCatName($this->security->xss_clean($subcat)); //gets subcatname using $subcat in URL
  $this->input->set_cookie('subcategid', $data['subcatid'], '0'); // sets subcategory id cookie
  $data['adnumber'] = $this->security->xss_clean($idnum);  
if ($cat == 'firehouses)
{
  $this->load->model('Mgetfirehouses'); // loads model
  $data['adinfo']  = $this->Mgetfirehouses->gettextbookInfo($data['colid'],$data['catid'],$data['caturl'],$data['adnumber']);
  $this->load->view('view_firehouses', $data);
}
elseif ($cat == 'shoppingmalls')  //display 'for sale' ads
{
  $this->load->model('Mgetmalls'); // loads model
  $data['adinfo']  = $this->Mgetmalls->getForSaleInfo($data['colid'],$data['catid'],$data['caturl'],$data['adnumber']);
  $this->load->view('view_shoppingmalls', $data);
}

}
}


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.