CodeIgniter Forums
Redirect to controller specific function w/parameters - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Redirect to controller specific function w/parameters (/showthread.php?tid=30910)



Redirect to controller specific function w/parameters - El Forum - 05-30-2010

[eluser]chitoso[/eluser]
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:

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

On my Admin Controller I have the function:

Code:
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!


Redirect to controller specific function w/parameters - El Forum - 05-31-2010

[eluser]mddd[/eluser]
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?


Redirect to controller specific function w/parameters - El Forum - 05-31-2010

[eluser]chitoso[/eluser]
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:
Code:
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.


Redirect to controller specific function w/parameters - El Forum - 05-31-2010

[eluser]mddd[/eluser]
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.