Welcome Guest, Not a member yet? Register   Sign In
Manipulate called controller function name before it is called?

Is there a way for me to change the incoming request function name before the function is actually called?

For instance, say the incoming request is to "domain.com/foo/bar", I want to be able to change the "bar" to something else and let CI call the function I give it instead of the "bar" it would usually call.

I've read about the _remap() possibility, but that won't work in my situation. What I want to do is basically setup a post_controller_constructor hook, and if the incoming request is a POST request, append "_post" to the function name CI is going to call. That way, I can have a form post to "domain.com/foo/bar", but the request will be routed to the "bar_post" function. I don't want to have to put _remap() in every controller.

Is there a way to do this?


Hi Taylor,

you have to define a route.

Put this line in your config/routes.php

$route['foo/bar'] = 'foo/bar_post';

In this case every request on controller "foo" with method "bar" (which don't have to exist) will be redirected to controller "foo" with method "bar_post".


Thanks for the response. However, I want HTTP GET request to really go to the foo/bar method. I only want HTTP POST requests to go to foo/bar_post. The route would force all requests (both GET and POST) to go to the foo/bar_post method.

class Foo extends Controller {
   function bar() {
      if ($this->input->post('form') !== false) {
      } else {
         // Form here?

   function bar_post() {
      // Post stuff here


That would work :p

Yeah, that is what I have been doing. I'm just trying to get away from doing that in all the functions that have POST counterparts. This is just an effort to clean up my controllers some by making it more automatic.

I personally wouldn't know how to do what you described automatically, but its certainly possible Smile Good luck finding the answer.

I figured out how to do it by adding a couple of lines of code in system/core/CodeIgniter.php, but I would like to do it without any hacking.

[eluser]Bart Mebane[/eluser]
You could extend the Controller class and write a generic _remap method.

Theme © iAndrew 2016 - Forum software by © MyBB