Welcome Guest, Not a member yet? Register   Sign In
Writing a proper _remap function
#1

[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?
#2

[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.
#3

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

here it is

cheers.




Theme © iAndrew 2016 - Forum software by © MyBB