CodeIgniter Forums
Retrieve controller namd and function name from url - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Retrieve controller namd and function name from url (/thread-50994.html)

Pages: 1 2


Retrieve controller namd and function name from url - El Forum - 04-16-2012

[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?


Retrieve controller namd and function name from url - El Forum - 04-16-2012

[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



Retrieve controller namd and function name from url - El Forum - 04-16-2012

[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.


Retrieve controller namd and function name from url - El Forum - 04-16-2012

[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?


Retrieve controller namd and function name from url - El Forum - 04-16-2012

[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...


Retrieve controller namd and function name from url - El Forum - 04-16-2012

[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.


Retrieve controller namd and function name from url - El Forum - 04-16-2012

[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.


Retrieve controller namd and function name from url - El Forum - 04-16-2012

[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];


Retrieve controller namd and function name from url - El Forum - 04-16-2012

[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)



Retrieve controller namd and function name from url - El Forum - 04-16-2012

[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'