Go to content Go to navigation Go to search


Browse

RSS / Atom

All Articles

Code!

Light my code up · 2007-04-18 11:12

Code Igniter is a pretty nifty little framework. Pretty much every time I go in to change something outside of the application I find there's a way I can do it without hacking on the core.

Take php's method overloading for example. Codeigniter is kind enough to provide an interface to it, meaning you can still overload functions without having to worry about changing the usual routing it employs.

Take stepping through pages of a multiple page form. You know, you have 100 questions to ask and split it up into 10 pages of 10 questions each.

So, if I want to simulate this:

  public function step1()
  {
    $this->runStep(1);
  }

  public function step2()
  {
    $this->runStep(2);
  }
....
I can stick this into my Controller and get the same effect without the repetition. Good thing too, no macros in PHP.
  protected function runStep($n)
  {
    // use data for step $n usefully
  }

  public function _remap($m)
  {
    global $RTR;
    $matches = array();

    if(preg_match('/step([0-9]+)/', $m, $matches) > 0)
      call_user_func_array(array(&$this, 'runStep'), array($matches[1]));
    elseif(!method_exists($this, $m))
      show_404();
    else
      call_user_func_array(array(&$this, $m), array_slice($RTR->rsegments, (($RTR->fetch_directory() == '') ? 2 : 3)));
  }
mah mah mah myyy controller

I've managed to abstract away the controller for such a section down to basically nothing but an array containing information on each step - title, fields, validation rules etc.

The beauty of this is that everything can be done in the parent class, while the child only needs information - what view to use and step information.

Of course we still have to write the views ourselves, no way of getting around that without having something dicky we'd have to change anyway (looking at you, scaffolding).

Commenting is closed for this article.

Chess photo comedy gold