Are you a regular stikked user? Signup so you can keep track of your pastes!

Usefull Base Controller

By Spockz, 2 Years ago, written in PHP.
URL http://stikked.com/view/fd9b0d2c
  1. <?php
  2. /**
  3.  * This is my own custom controller. It adds support for .extension in the url, and it provides a shortcut to the view function. So that in the children you wont have to type so much.
  4.  *
  5.  */
  6. class My_Controller extends Controller {
  7.   /**
  8.    * Contains the layout/extension for the current view.
  9.    *
  10.    * @var string
  11.    */
  12.   protected
  13.     $layout = 'xhtml';
  14.    
  15.   protected
  16.     $cssFiles = array(),
  17.     $jsFiles  = array();
  18.    
  19.   /**
  20.    * Controls startup options.
  21.    *
  22.    */
  23.   function __construct() {
  24.     parent::__construct();
  25.     $this->output->enable_profiler();
  26.    
  27.     $this->load->library('My_Library', array());
  28.   }
  29.  
  30.   /**
  31.    * This function remaps the url and retrieves the desired extension from it. It then calls the appropiate controller method.
  32.    *
  33.    * @param string $aMethod
  34.    */
  35.   function _remap($aMethod) {
  36.     $lParams = $this->uri->segment_array();
  37.     array_shift($lParams);array_shift($lParams);
  38.    
  39.     $lMethod = $aMethod;
  40.     if (strpos($lMethod, '.') !== False)
  41.       list($lMethod, $this->layout) = explode('.', $lMethod);
  42.      
  43.     $lMethod = (empty($lMethod) ? 'index' : $lMethod);
  44.    
  45.     if (method_exists($this, $lMethod)) {
  46.       call_user_func_array(array($this, $lMethod), $lParams);
  47.     } else {
  48.       show_404($lMethod);
  49.     }
  50.   }
  51.  
  52.   /**
  53.    * This function provides a shortcut to the load->view function. It also appends the current layout so you won't get lost.
  54.    *
  55.    * @param string $aView
  56.    */
  57.   function view($aView, $aData=array()) {
  58.    
  59.     $aData['cssFiles'] = $this->cssFiles;
  60.     $aData['jsFiles']  = $this->jsFiles;
  61.     $this->load->view($this->layout.DIRECTORY_SEPARATOR.$aView, $aData);
  62.   }
  63.  
  64.   /**
  65.    * Uses the last segment to read the querystring. Use this to relieve you from the absence of GET in CI.
  66.    *
  67.    * @return array
  68.    */
  69.   function getQuery() {
  70.     $segs = $this->uri->total_segments();
  71.     if ($segs>2) {
  72.       $qs = $this->uri->segment($segs);
  73.       $data = array();
  74.       parse_str($qs, $data);
  75.       return $data;
  76.     } else {
  77.       return null;
  78.     }
  79.   }
  80.  
  81.   /**
  82.    * This function is to register the css files you want to use with this controller.
  83.    *
  84.    * @param string $aCSSFile
  85.    */
  86.   function registerCSSFile($aCSSFile) {
  87.     $this->cssFiles[] = $aCSSFile;
  88.   }
  89.  
  90.   /**
  91.    * This function is to register the javascript files you want to use with this controller.
  92.    *
  93.    * @param string $aJSFile
  94.    */
  95.   function registerJSFile($aJSFile) {
  96.     $this->jsFiles[] = $aJSFile;
  97.   }
  98. }
  99. ?>

Reply to "Usefull Base Controller"

Here you can reply to the paste above

Create a snipurl

Make Private

Feeling clever? Set some advanced options.