CodeIgniter Forums
Issue with routing when running from the CLI - 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: Issue with routing when running from the CLI (/thread-45655.html)



Issue with routing when running from the CLI - El Forum - 09-29-2011

[eluser]Unknown[/eluser]
I'm trying to run a script via the CLI, but am having some issues getting it to route to the proper controller.

Here is my routes.php file:

Code:
$route['default_controller'] = "home";
$route['scaffolding_trigger'] = "scaffolding";

// URI like '/en/about' -> use controller 'about'
$route['^fr/(.+)$'] = "$1";
$route['^en/(.+)$'] = "$1";

// '/en' and '/fr' URIs -> use default controller
$route['^fr$'] = $route['default_controller'];
$route['^en$'] = $route['default_controller'];

The function I'm trying to call is on my non-default controller. To access it via the web, I can use this URL and it works fine: http://localhost/mobile.php/en/mycontroller/myfunction

Two things to note here (not sure if they matter):

1) We are using a non-default main php file of mobile.php instead of index.php

2) We do not have an .htaccess file set up, so we are just using the mobile.php directly in the URL.

Now, when I try to access this same function via the CLI, it is always loading up the default controller:

Code:
php mobile.php en mycontroller myfunction

I have tried this with and without the "en". Any ideas what I'm doing wrong here?


Issue with routing when running from the CLI - El Forum - 04-08-2013

[eluser]bigmac[/eluser]
I see that it has been well over a year since the last post. Has any progress been made?

I am attempting to use the CLI interface as well and am not getting anywhere. The Router simply does not see the command line and thus does not construct the request. Is this partially a PHP configuration issue? (Something I will look into).

Any constructive thought on the issue would be appreciated.


Issue with routing when running from the CLI - El Forum - 04-08-2013

[eluser]CroNiX[/eluser]
Not positive, but it could be that routes are relying on a $_SERVER attribute being set, such as REQUEST_URI or similar, which isn't set when used via the CLI since it's not a web request. Using the full unrouted controller/method/etc should work via the CLI the way the user guide shows.


Issue with routing when running from the CLI - El Forum - 04-08-2013

[eluser]bigmac[/eluser]
Correct - the _SERVER environment is simply missing and not replicated by PHP or CI.

The solution for me was to simply set the config var 'uri_protocol' to 'AUTO' - and the web access continues to function. The User Guide CLI example fails, reliably, if this isn't done this way..

Also found a spot in core/Input.php (line 351) where it attempts to determine the IP address the request is sourced from. Same problem.

Cheers