Welcome Guest, Not a member yet? Register   Sign In
$this->_remap('home') sends index instead of home.
#1

[eluser]Bramme[/eluser]
Hi all


In my frontend controller, I use a _remap function that checks if pages are created in a database or if they're existing methods, else, I show a 404 error.

This has worked fine before, but all of a sudden, I noticed a bug.

here's my index function:

Code:
function index() {
    $this->_remap('home');
}

function _remap($method) {
// some more code
}

My remap functions handles all the rest, however, suddenly I noticed mysite.com/ was empty, but mysite.com/home works perfectly fine.

I examined the variables and noticed that when visiting mysite.com/ $method isn't 'home' (like I was trying to send with my index function) but just 'index'.

Is this some new behaviour, a bug or does it simply mean i have to put
Code:
if($method == 'index') $method = 'home'
in my remap function?
#2

[eluser]TheFuzzy0ne[/eluser]
I don't understand the need for your index method to call _remap. _remap is called automatically by CodeIgniter. Also, I don't know if your production code is like you've written above, but you'd get a notice each time _remap is called by CodeIgniter, as no argument is passed to the method, but one is expected.
#3

[eluser]Bramme[/eluser]
The overridden function call (typically the second segment of the URI) will be passed as a parameter the _remap() function:
Code:
function _remap($method)
{
    if ($method == 'some_method')
    {
        $this->$method();
    }
    else
    {
        $this->default_method();
    }
}

So far, i'm right :p but you might be right about the index method...
#4

[eluser]TheFuzzy0ne[/eluser]
It might become clearer if you post all of your _remap code instead of just describing it. Sorry, I'm just so confused now. Big Grin
#5

[eluser]Bramme[/eluser]
Oh, right.

Here's the code:

Code:
function _remap($method){

    if($method == 'index') $method = 'home';

    $pages = $this->mdl_content->get_pages();
    
    if ($pages !== FALSE)
    {
        foreach ($pages as $key => $page)
        {
    
            if (strtolower(url_title($page['title'])) == strtolower(url_title($method)))
            {
                $this->load->model(array('mdl_sidebar', 'mdl_gallery'));
                
                $content = $this->mdl_content->place_galleries($page['content']);
            
                $this->template->write('_content', $content);
                $data['items'] = $this->mdl_sidebar->get_items($key);
                $this->template->write_view('_sidebar', 'sidebar', $data);    
                    
                $this->template->add_js('assets/js/jquery.js');
                $this->template->add_js('assets/js/galleryview/jquery.timers.js');
                $this->template->add_js('assets/js/galleryview/jquery.galleryview.js');
                $this->template->add_js('assets/js/galleries.js');
                $this->template->add_css('assets/css/galleryview.css');
                
                $this->template->render();
                return;
            }
        }
    }
    elseif (method_exists($this, $method))
    {
        $this->$method();
    }
    elseif ($pages === FALSE)
    {
        show_error('I can\'t find any pages yet!');
    }
    else
    {
        show_404($method);
    }
}
Basically, it checks the database first to see if any of the pages in the database match the requested page, if not, it checks the other methods in the frontend controller (handy for pages that need further processing: news, contact etc...), if none of those are found (because there were no pages in the db) an error is shown that there are no pages yet and if none of those are met, show a 404 error.
#6

[eluser]TheFuzzy0ne[/eluser]
I think that's your solution. As I mentioned, index() is never actually called, which is why it's not working as you'd expect.

From what I see, the code above should work exactly as expected.
#7

[eluser]Bramme[/eluser]
It's strange, because in another app (that has the same code, except for the first if that's missing), i use an index method that does the $this->_remap('home') thingie and the _remap function works well.
#8

[eluser]TheFuzzy0ne[/eluser]
That is very strange, as once _remap() is called, no other methods are unless you call them from within your function. I can't help thinking that you're just assuming this is the case when it's something in your code that makes it work correctly, but not as expected.

I can only assume that "home" is being passed via the URI, where as in this case, it's not.
#9

[eluser]jdfwarrior[/eluser]
Index should not be called. When the remap function is used, it replaces index as the default function. If you dont specify a function for that controller, it still reads "index" as the function to be called, from _remap.

So if you want it to call the index function you need to have:

Code:
function _remap($func) {

    switch ($func) {
        case "index": $this->index();

    }

}

You have to explicity call index.




Theme © iAndrew 2016 - Forum software by © MyBB