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

#1
[eluser]coldscooter[/eluser]
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?

#2
[eluser]CroNiX[/eluser]
Code:
echo $this->router->fetch_directory();  //current directory
echo $this->router->fetch_class();      //current classname
echo $this->router->fetch_method();     //current method

#3
[eluser]coldscooter[/eluser]
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.

#4
[eluser]CroNiX[/eluser]
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?

#5
[eluser]Aken[/eluser]
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...

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

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

#8
[eluser]coldscooter[/eluser]
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];

#9
[eluser]C.T.[/eluser]
if you wish to get the client URL

try using URI helper instead?
Code:
$this->uri->segment(1)
$this->uri->segment(2)
$this->uri->segment(3)

#10
[eluser]coldscooter[/eluser]
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:

cisite.com/controller1/index
http://cisite.com/controller1/
/controller1/

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.