• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Retrieve controller namd and function name from url

I am passing a local url to a function in codeigniter as a $_GET parameter. I want to find the controller name and method of the url. I can't just explode by '/' as sometimes i don't specify the function name and allow it to use the index function.

Any ideas?

echo $this->router->fetch_directory();  //current directory
echo $this->router->fetch_class();      //current classname
echo $this->router->fetch_method();     //current method

Thanks, but this retrieves data about the requested url. I want to determine the controller and method name of a url string, which may be different from the current.

There's nothing that will tell you that from a string. And if there was it would probably be really inefficient. How would knowing this benefit you? What are you trying to accomplish?

CroNIX's method returns the directory, controller and method that is currently being requested, regardless of what the URL says. Sounds like that's exactly what you want...

I'm creating a controller to handle ajax calls. I want it to load a library that uses the current controller/method as keys to retrieve particular files. So i just want to be able to parse a url string and pass it what it needs.

To clarify: i am posting the 'action' of a form to an ajax controller, and I use that to validate that the data is valid for the controller to receive.

This is how i'm handling it at the moment, but i was just hoping for a more elegant CI solution.

$formAction = trim(str_replace(site_url(), '', $_REQUEST['formAction']), '/');
$segments = explode('/', $formAction);

$formActionController = $segments[0];
$formActionMethod = (empty($segments[1]))?'index':$segments[1];

if you wish to get the client URL

try using URI helper instead?

It's not the client uri or the url i'm after. It's a string of link into the CI site, so could be of various form. Example:


From all these strings, i'd like to be able to determine controller name and method name.

For all 3 it should be 'controller1' and 'index'

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.