2

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';
Pradeep
  • 9,667
  • 13
  • 27
  • 34
user9437856
  • 2,360
  • 2
  • 33
  • 92

2 Answers2

3

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

Pradeep
  • 9,667
  • 13
  • 27
  • 34
  • Yes, It's working. How is it working? can you help me out? – user9437856 Jun 15 '18 at 12:23
  • as i told you by default The built in $route['default_controller'] will not work for sub-folders. you have to extend system router as per your requirements – Pradeep Jun 15 '18 at 12:25
  • my default controller is working using your MY_Router code but how to access the backend controller? – user9437856 Jun 15 '18 at 12:27
  • Yes, sure I will accept it and upvote it your answer, but give me some time to implement in my code. default_controller is working but $config['backend'] = 'backend/Access_control'; is not working – user9437856 Jun 15 '18 at 12:36
  • Oh!! God I added $config instated of the route. that's the reason it's not working. Now It's working perfectly. thanks and upvote from my side. – user9437856 Jun 15 '18 at 12:38
  • pleasure is mine , happy coding – Pradeep Jun 15 '18 at 12:39
  • I just want to know where should I add the .htaccess file inside the application for outside application? which is the best? – user9437856 Jun 15 '18 at 12:40
  • outside the application folder parallel to example_ci_row folder – Pradeep Jun 15 '18 at 12:41
  • Cool!! Thank you – user9437856 Jun 15 '18 at 12:42
  • I hope you are doing well, I know it's not a good way to redirect any member to the question but trust me I am solving one issue from the last 4 days. if you know any think on this please let me know. here is the link https://stackoverflow.com/questions/61977036/how-to-display-the-array-inside-array-output – user9437856 May 25 '20 at 10:48
  • Yes, You suggested to me and that is working. I just need to know if I have to access the samename value then how can I access it? I tried print_r($row->somename[0]->disbursed); but it's not working. – user9437856 May 25 '20 at 12:58
  • @user9437856 you got to access like this `foreach($value->somename as $item){ echo $item->disbursed;}` – Pradeep May 25 '20 at 15:51
  • Thanks for this, I am already in foreach and I tried $row->somename[0]->filesubmit again and it's working – user9437856 May 26 '20 at 03:39
0

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

$config['base_url'] = 'http://localhost/example_ci_row';
NSJ
  • 13
  • 7
  • I think, There might be some issue with routes – user9437856 Jun 15 '18 at 11:51
  • To fix other issues we need to clarify that default controller is working. When you try `http://localhost/example_ci_row` in your address bar, it cannot display any error according to these settings. – NSJ Jun 15 '18 at 11:56
  • Yes, default controller is working but It is working when I add the controller outside of the frontend and backend folder. – user9437856 Jun 15 '18 at 11:59
  • I found an previous answer here for your problem. Follow this https://stackoverflow.com/questions/6529026/codeigniter-default-controller-in-a-sub-directory-not-working – NSJ Jun 15 '18 at 12:05
  • I already tried that before asking the question. If I hit the URL http://localhost/example_ci_row/index.php/frontend/User_control then I am getting the my login page but When I hit the URL http://localhost/example_ci_row/ then It's getting 404 page not found – user9437856 Jun 15 '18 at 12:07
  • Have you created a `.htaccess` file and configured config file like this; `$config['index_page'] = ' ';` ? – NSJ Jun 15 '18 at 12:11
  • Yes, I have created the .htaccess inside my root(example_ci_row) folder and $config['base_url'] = 'http://localhost/example_ci_row'; $config['index_page'] = 'index.php'; My .htaccess file is emply now. should I add any code? – user9437856 Jun 15 '18 at 12:14
  • `$config['index_page'] = 'index.php';` this should be like this `$config['index_page'] = '';` You should remove `index.php` that is there inside `''` – NSJ Jun 15 '18 at 12:17
  • Yes, I remove the index.php and added code in the htacess which you provided but still I am not able to access the $route['default_controller'] = 'frontend/User_control'; still getting 404 page not found – user9437856 Jun 15 '18 at 12:18