RJ Tengco
RJ Tengco

Reputation: 1

My Custom Routing Is Not Working in Codeigniter 3

Hi my custom routing is not working. When i type http://localhost/sitename the default_controller routing is working but when i type http://localhost/sitename/test the browser output is 404 not found. Please help me thank you.

$route['default_controller'] = 'Traffic/test';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['test'] = "Traffic/test";

Upvotes: 0

Views: 158

Answers (3)

noor alam
noor alam

Reputation: 29

Please try this code on the routes.php

$route['default_controller'] = 'welcome';
$route['test'] = 'traffic/test'; 
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

Upvotes: 1

Javier Larroulet
Javier Larroulet

Reputation: 3237

Please note that $route['default_controller'] expects a controller not a controller/method pair. The first thing you need to do is change that to $route['default_controller'] = 'traffic';

You may have some success using a controller/method pair, but you may run into trouble in the future as your routing increases in complexity.

also, as someone else already noted, Codeigniter naming and case convention must be followed: Even though the controller filename is uppercase (i.e., Traffic.php) and the controller class is uppercase too (class Traffic extends CI_Controller) whenever you make a reference to the controller such as in the default_controller route, you must go all lowercase.

That said, your correct routing configuration should be:

$route['default_controller'] = 'traffic';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['test'] = "traffic/test";

With this configuration:

  • Browsing to example.com will load https://example.com/traffic/index
  • Browsing to example.com/test will load https://example.com/traffic/test
  • Browsing to any other URI will attempt to load the controller/method pair according to standard Codeigniter routing (i.e., example.com/something/trial will load the something controller and the trial method within the former)

Upvotes: 0

Mujahid Bhoraniya
Mujahid Bhoraniya

Reputation: 1584

I had to rename my default controller php file to lowercase and the controller class name to lowercase and everything started to work. When CI looks for the default controller file, it searches in lowercase for the file; if I name my controller file "Traffic/test" instead of "traffic/test"

$route['default_controller'] = 'traffic/test';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['test'] = "traffic/test";

now above code copy and paste your routes.php

Upvotes: 0

Related Questions