CodeIgniter default controller in subdirectory not working

My default_controller in the route configuration is set to "home.php".

I have an auxiliary directory for my controllers, which can be called a "folder". So if I am http://mysite.com/folder/ , then by default the controller "folder / home.php" should be called correctly?

However, for some reason this does not work, I get 404. A visit to http://mysite.com/folder/home or http://mysite.com/folder/home/index works as expected. In addition to this, the controller works by default in the root directory ( http://mysite.com loads home.php).

Any ideas, anyone else experienced this? I cannot think it over - it will be a CI problem, but I cannot find anyone else with the same problem.

The documentation, as I understand it, at least suggests that this should work fine: http://codeigniter.com/user_guide/general/controllers.html#subfolders

Setting the default controller to "folder / home.php" means that http://mysite.com/folder/ is working fine as expected. Except that the default controller should simply be "home.php" - both in the root and in the subdirectory, home.php inside this directory should be loaded, as the documentation suggests.

Greetings

+9
php codeigniter
source share
6 answers

For each subfolder in the controllers folder, you must specify the default controller in routes.php . The built-in $route['default_controller'] will not work for subfolders.

for example: to set the default controller for the folder subfolder for home add the following to the /application/config/routes.php file:

 $route['folder'] = "folder/home"; 

which means that http://mysite.com/folder/ same as http://mysite.com/folder/home as a URL.

+23
source share

You can expand the system router to meet the requirements,

  • Create the My_Router.php directory in the application/core/ directory

/ * * To change this license header, select "License Headers" in the "Project Properties". * To edit this template file, select "Tools | Templates * and open the template in the editor. * /

 /** * Description of My_Router * * @author girish */ class My_Router extends CI_Router { //put your code here public function __construct($routing = NULL) { parent::__construct($routing); } protected function _set_default_controller() { if (empty($this->default_controller)) { show_error('Unable to determine what should be displayed. A default route has not been specified in the routing file.'); } // Is the method being specified? if (sscanf($this->default_controller, '%[^/]/%[^/]/%s', $directory, $class, $method) !== 3) { $method = 'index'; } if (is_dir(APPPATH . 'controllers' . DIRECTORY_SEPARATOR . $directory) === true) { if (!file_exists(APPPATH . 'controllers' . DIRECTORY_SEPARATOR . $directory . DIRECTORY_SEPARATOR . ucfirst($class) . '.php')) { // This will trigger 404 later return; } $this->set_directory($directory); $this->set_class($class); $this->set_method($method); } else { if (sscanf($this->default_controller, '%[^/]/%s', $class, $method) !== 2) { $method = 'index'; } if (!file_exists(APPPATH . 'controllers' . DIRECTORY_SEPARATOR . ucfirst($class) . '.php')) { // This will trigger 404 later return; } $this->set_class($class); $this->set_method($method); } // Assign routed segments, index starting from 1 $this->uri->rsegments = array( 1 => $class, 2 => $method ); log_message('debug', 'No URI present. Default controller set.'); } } 

and overwrite _set_default_controller() from the user method, it will work with the subdirectory controller as well as with the root controller.

And in application/config/routes.php

if you need a default controller for a subdirectory then

  $route['default_controller'] = "admin/admins/login"; 
  • admin - folder
  • admins - controller
  • login - method

If you need a default root directory, then

  $route['default_controller'] = "welcome/index"; 
  • welcome - controller
  • index - method

not sure if it will work in all versions, but tested in CI3.0.6

+12
source share

If you want to remain flexible, you need to pass everything after the initial folder (in application/config/config.php ):

 $route['home'] = "home/whatever"; $route['home/(:any)'] = "home/whatever/$1"; 
+4
source share

The default route is used to indicate to the CI which controller class should be loaded if the URI contains no data.

enter image description here

 $route['default_controller'] = "unicorn/best"; 

So when i load

 http://example.com/index.php/unicorn/ 

The best controller will be loaded.

also at boot

 http://example.com/ 

or

 http://example.com/index.php/horse/ 

The best controller will be loaded.

+1
source share

MY FALL STRUCTURE

 --controllers --backend --frontend --home.php --products.php --productDetail.php --homeIndex.php 

In config / routes.php

 $route['default_controller'] = 'homeIndex'; $route['frontend'] = 'frontend/home'; $route['backend'] = 'backend/home'; 

In the controllers /homeIndex.php

 <?php defined('BASEPATH') OR exit('No direct script access allowed'); require_once(APPPATH.'controllers/frontend/Home.php'); class homeIndex extends home { public function index() { $this->action(); } } 

by default, homeIndex will be loaded and from homeIndex I will call the function frontend / home / action.

0
source share

Add this line to application / config / rout.php

 $this->set_directory( "yourfoldername" ); $route['default_controller'] = 'controller name'; 
0
source share

All Articles