CodeIgniter default controller in a sub directory not working

前端 未结 9 1638
醉话见心
醉话见心 2020-11-29 10:15

My default_controller in the routes configuration is set as \"home.php\".

I have a sub directory for my controllers, lets call it \"folder\". So if I visit http://my

相关标签:
9条回答
  • 2020-11-29 11:17

    Default route is used to tell 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 when I load

    http://example.com/
    

    or

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

    the best controller will be loaded.

    0 讨论(0)
  • 2020-11-29 11:20

    You can extend system router as per requirements,

    1. Create My_Router.php in application/core/ directory

    /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose 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 custom method, it will work from sub-directory controller as well root directory controller.

    And in application/config/routes.php

    if you need sub-directory default controller, then

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

    if you need root-directory default controller, then

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

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

    0 讨论(0)
  • 2020-11-29 11:20

    MY FOLDER 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 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 call to frontend/home/action function.

    0 讨论(0)
提交回复
热议问题