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

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

Thanks!
#2

[eluser]Deveron[/eluser]
Hi Taylor,

you have to define a route.

Put this line in your config/routes.php

Code:
$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".


Cheers
#3

[eluser]TaylorOtwell[/eluser]
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.
#4

[eluser]dudeami0[/eluser]
Code:
class Foo extends Controller {
  
   function bar() {
      if ($this->input->post('form') !== false) {
         $this->bar_post();
      } else {
         // Form here?
      }
   }

   function bar_post() {
      // Post stuff here
   }

}

That would work :p
#5

[eluser]TaylorOtwell[/eluser]
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.
#6

[eluser]dudeami0[/eluser]
I personally wouldn't know how to do what you described automatically, but its certainly possible Smile Good luck finding the answer.
#7

[eluser]TaylorOtwell[/eluser]
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.
#8

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




Theme © iAndrew 2016 - Forum software by © MyBB