CodeIgniter Forums
Writing a proper _remap function - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Writing a proper _remap function (/showthread.php?tid=14190)



Writing a proper _remap function - El Forum - 12-22-2008

[eluser]Bramme[/eluser]
Hey all. I'm working on a little website that has some static pages, but most of the content is stored in the database. I thought of using one front end controller, with functions for each of the static pages and then a _remap function to catch the calls to the the database stored pages.

Here's how my function looks right now:

Code:
function _remap($method){
    $pages = $this->mdl_content->get_pages();
    foreach ($pages as $key => $page)
    {
        if (strtolower(url_title($page['title'])) == strtolower(url_title($method)))
        {
            $this->template->write('_content', $page['content']);
            $this->template->render();
            return;
        }
    }
    if (method_exists($this->$method()))
    {
        $this->$method();
    }
    else
    {
        show_404($method);
    }
}
However, if I enter a faulty method (like "foobar") I still get an error "Call to undefined function foobar() on line 25"
Line 25 happens to be the "if (method_exists())" check.

How do I solve this?


Writing a proper _remap function - El Forum - 12-22-2008

[eluser]Bramme[/eluser]
Okay, silly me. I just didn't read the method_exists manual properly...

It's supposed to be "if (method_exists($this, $method))". Now it works like a charm.

However, feel free to look at my _remap function and point out if anything could be done better.


Writing a proper _remap function - El Forum - 01-05-2009

[eluser]sophistry[/eluser]
see site_migrate in the wiki for a general purpose solution to this...

here it is

cheers.