CodeIgniter Using _remap() and Named Methods

One of the most important methods I’ve found in CodeIgnitor is the _remap() method. When you use it within a controller it hiijacks all other methods and does your bidding.

This is great when you have a controller Article.php and you want the 2nd value in your urls to be the ID number or a unique url encoded title or ‘slug’ for the article being queried. You can’t be expected to build a method each time you publish an article. Plus you’ll likely want your _remap() method to query a database and display the article that way.

The problem I’ve run into a few times is that if I want to use both _remap() and named functions it is a hassle.

Example: With my article example if I want a url and page: /article/submit or /article/search, while still having /article/how-to-sew-curtains and /article/writing-smart-codeignitor-controllers query from the database based on the string after /article/ I have had to build exceptions within the _remap() method to redirect explicitly to other methods withing the controller class.

To better outline that example:

These urls should access their methods in the Article (submit and search, respectively:

  • http://example.com/article/submit
  • http://example.com/article/search

These urls are the reason we have to use _remap() to begin with cause they are dynamic based on the contents of a database:

  • http://example.com/article/how-to-sew-curtains
  • http://example.com/article/writing-smart-codeignitor-controllers

Then I found a better solution:

File: Article.php

<?php
class Article extends Controller {

    function Article()
    {
        parent::Controller();

    function _remap($method)
    {
        if(method_exists($this,$method))
        {
            call_user_func(array($this, $method));
            return false;
        }

       /*
        * Call to the DB to see if the article exists, if it does, serve it
        * I'd recommend building this in a seperate method and just calling it here
        * like so:
        */

        if($this->getArticle())
            $this->displayArticle();
        else
            $this->errorArticle();



    }

    function search()
    {
       /*
        * Build search functionality here
        */
    }

    function submit()
    {
       /*
        * Build submit functionality here
        */
    }

    function getArticle()
    {
       /*
        * Use the DB class to fetch the article
        * return false if article doesn't exist or isn't published yet or whatever
        */
    }

    function displayArticle()
    {
       /*
        * Just rendering here
        */
    }

    function errorArticle()
    {
       /*
        * Output error page (don't forget your 404 heaaders)
        */
    }
}

This amazing snippet of code will first check to see if a method or function by that name ($method) exists within the current class($this), and if it does it will use that method. We return false immediately after we call that method using PHP’s built in call_user_func() function so nothing else in the _remap() method is executed. Otherwise you can remap as usual!

Note: You could also use CodeIgniter’s URI Routing http://codeigniter.com/user_guide/general/routing.html but that’s not something I’m terribly familiar with so I’ll stick to _remap() for the time being!

This site runs on the Thesis WordPress Theme

Thesis Theme thumbnail

If you're someone who doesn't understand a lot of PHP, HTML, or CSS, Thesis will give you a ton of functionality without having to alter any code. For the advanced, Thesis has incredible customization possibilities via extensive hooks and filters. And with so many design options, you can use the template over and over and never have it look like the same site.

If you're more familiar with how websites work, you can use the fantastic Thesis User's Guide and world-class support forums to make more professional customizations than you ever thought possible. The theme is not only highly customizable, but it allows me to build sites with a much more targeted focus on monetization than ever before. You can find out more about Thesis below:

{ 1 comment }

1 Julio Mingot Rotulos Electronicos June 14, 2011 at 6:57 am

Hola me gusta mucho lo que has escrito

Comments on this entry are closed.

Previous post:

Next post: