ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Redirect to controller specific function w/parameters

May 30, 2010 4:58pm

Subscribe [2]
  • #1 / May 30, 2010 4:58pm

    chitoso

    7 posts

    Hi guys, this is my first time here. I’ve been learning CI for a quick project. It’s been a couple of days now and I think it was a great choice so far.

    Anyway, I have a specific doubt that couldn’t solve in this few days.

    In my routes.php:

    $route['admin/pages/edit/(:num)'] = 'admin/admin/edit_page/$1';

    On my Admin Controller I have the function:

    function edit_page($page_id) {
    ...
    
    }

    And in some point in edit_page I I’m doing some redirects like: redirect(‘admin/pages/edit/’ . $page_id)

    But, I’d feel it much more natural to do this: redirect(‘admin/admin/edit_page/’, $page_id). This way, URLs wouldn’t have to be so coupled in the code.

    Is the first usage the way we’re supposed to do this or am I missing something?

    Thanks!

  • #2 / May 31, 2010 4:15am

    mddd

    635 posts

    The redirect() function takes other arguments: after the location you can specify the method and the http status code. So, yes your first example is correct. The url must be in the first argument completely.

    I can understand that you’d like to keep things as separate as possible. But on the other hand it is more of a visual difference than anything else right?

  • #3 / May 31, 2010 11:50am

    chitoso

    7 posts

    Thanks for the reply.

    Well, the main reason would be not to depend on urls.

    If I can do a redirect directly to the controller function, like:

    redirect('admin/admin/edit_page/', $id)

    , then I could change urls on routes.php without having to change all my redirects afterwards.

    Anyway, it’s good to know I’m on the right track.

  • #4 / May 31, 2010 12:06pm

    mddd

    635 posts

    Oh, I think I see what you mean now. You mean that CI would call the function in the right controller immediately?
    That would give all kinds of problems. For instance: what would be the right values for $this->uri? What would happen with caching?
    No, if you use redirect() it sends a header() command and completely reloads the page. It’s not happening inside CodeIgniter, but basically making a brand new request. And therefore it will always go through .htaccess and all your routes again.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases