user9437856
user9437856

Reputation: 2388

CodeIgniter routes issues to access the frontend and backend folder

I am using CodeIgniter. I have frontend and backend folder inside controllers and views. I tried server steps even check almost all the solution but still I am not able to access it my default controller

enter image description here

routes.php

$route['default_controller'] = 'frontend/User_control';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

/*backend*********************************/
  $config['backend'] = 'backend/Access_control';  

1) My issue is When I am accessing the Url http://localhost/example_ci_row/

I am getting 404 Page not found

enter image description here

2) How to access the backend URL I tried http://localhost/icube_row/admin but I am getting the error

enter image description here

frontend User_control

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User_control extends CI_Controller {
    public $current_date;
    function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->helper('form');
    }
    public function index(){
        $this->load->view('frontend/login');
    }
    }
?>

Backend Access control

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Access_control extends CI_Controller {
    function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->helper('form');           
    }

    public function index(){
        $this->load->view('backend/login');
    }

    }

?>

Edited

It's working when I use below steps. I added the Test.php file in the controller and change the routes then I am getting the login page.

Test.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends CI_Controller {
    public $current_date;
    function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->helper('form');
    }
    public function index(){
        $this->load->view('frontend/login');
    }
    }
?>

routes.php

$route['default_controller'] = 'Test';

Upvotes: 2

Views: 2004

Answers (2)

Pradeep
Pradeep

Reputation: 9717

Hope this will help you :

The built in $route['default_controller'] will not work for sub-folders. you have to extend system router as per your requirements like this :

You need to create a MY_Router.php in application > core > 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.');
    }
}

This will allow you to use $route['default_controller'] = 'frontend/User_control'; as your default controller

Upvotes: 3

NSJ
NSJ

Reputation: 13

Have you configured the Base URL ? Application->config->config.php

$config['base_url'] = 'http://localhost/example_ci_row';

Upvotes: 0

Related Questions