Manish Tiwari
Manish Tiwari

Reputation: 1866

How to routing controllers in sub folders using codeigniter 3?

I have create an controller in subfolder of controller folder and in subfolder i create an login controller.

But i couldn't access this controller and already given rule in route file.

Structure :

Controller
--admin
   ---login.php

Route Rule :

$route['default_controller'] = 'admin/login';
$route['admin_login'] = 'admin/login';

Upvotes: 2

Views: 9030

Answers (3)

israfilgazi
israfilgazi

Reputation: 11

By default codeIgniter 3 versions and up you can not use sub folders in your default controller route.

To be able to use a sub folder in default_controller you need to use a MY_Router.php

$route['default_controller'] = 'home';
application >

application > core >

application > core > MY_Router.php

MY_Router.php


Controller
   --Admin /// this Sub folder
   ----Home.php //for admin home
   ----login.php
   ----setting.php

   defoult folder 
   -home.php  //for frond page
   -post.php
   -cat.php
//and========== routers
$route['default_controller'] = 'home';
 /* working doun 100% */

Upvotes: 0

DxGx
DxGx

Reputation: 9

Route: "admin/login" should work by default Codeigniter's config.

//WRONG
$route['admin_login'] = 'admin/login'; 

//GOOD
$route['adm/log'] = 'admin/login'; 
//!!!only needed if you want to change URL path, as CI works by default that way

Also please check if you:

  • properly created controller class at login.php
  • you have to have index() method in controller class
  • your class MUST be named with first letter capital ex. "Login" NOT "login"

Docs on Codeigniter Controllers

Upvotes: 0

user4419336
user4419336

Reputation:

By default codeIgniter 3 versions and up you can not use sub folders in your default controller route.

To be able to use a sub folder in default_controller you need to use a MY_Router.php

$route['default_controller'] = 'admin/login';

application >

application > core >

application > core > MY_Router.php

MY_Router.php

<?php

class MY_Router extends CI_Router {
    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', $class, $method) !== 2) {
            $method = 'index';
        }

        // This is what I added, checks if the class is a directory
        if( is_dir(APPPATH.'controllers/'.$class) ) {

            // Set the class as the directory

            $this->set_directory($class);

            // $method is the class

            $class = $method;

            // Re check for slash if method has been set

            if (sscanf($method, '%[^/]/%s', $class, $method) !== 2) {
                $method = 'index';
            }
        }

        if ( ! file_exists(APPPATH.'controllers/'.$this->directory.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.');
    }
}

Also make sure your you follow the codeIgniter way of naming files

File name: Login.php

<?php 

class Login extends CI_Controller {
    public function index() {

    }
}

Upvotes: 5

Related Questions