0

I use Codeigniter 3.0. I found that not able to route to file in subfolder. After, i edit file route.php in system/core like this guide : default controller inside subfolder codeigniter 3 not working Page could routing right. But, i get error after upload host like that : "Unable to load the requested file: frontend/Home.php" my structure :

-controllers
  -frontend
     Home.php
-views
  - frontend
     Home.php

My route file :

$route['default_controller'] = 'frontend/Home';

I checked name of class, up-case the first character.

Community
  • 1
  • 1
Ryan Tran
  • 467
  • 6
  • 16
  • "Unable to load the requested file" would seem to be it's the the *view* not loading; if it was the controller - it would be a 404 error message. Double check if you have the syntax right for the view load `$this->load->view( "frontend/home" );` – MackieeE Mar 25 '16 at 15:23

1 Answers1

0

CodeIgniter looks for Frontend controller with home method. You can solve it on two ways. First iw to make Home.php controller in controllers directory with next code:

<?php defined('BASEPATH') OR exit('Restricted area!');

class Frontend extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $this->home();
    }

    public function home()
    {
        redirect('frontend/home', 'refresh');
    }
}

with APPPATH.'config/routes.php' code

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

Second way is to use custom made router class:

<?php

/*
 * Custom router function v 0.1
 *
 * Add functionality : read into more than one sub-folder
 *
 */

class MY_Router extends CI_Router
{
    public function __construct()
    {
        parent::__construct();
    }

    public function _validate_request($segments)
    {
        if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
        {
            return $segments;
        }

        if (is_dir(APPPATH.'controllers/'.$segments[0]))
        {
            $this->set_directory($segments[0]);
            $segments = array_slice($segments, 1);

            /* ----------- ADDED CODE ------------ */

            while(count($segments) > 0 && is_dir(APPPATH.'controllers/'.$this->directory.$segments[0]))
            {
                // Set the directory and remove it from the segment array
                $this->set_directory($this->directory . $segments[0]);
            $segments = array_slice($segments, 1);
            }

            /* ----------- END ------------ */

            if (count($segments) > 0)
            {
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
                {
                    show_404($this->fetch_directory().$segments[0]);
                }
            }
            else
            {
                $this->set_class($this->default_controller);
                $this->set_method('index');

                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
                {
                    $this->directory = '';
                    return array();
                }

            }

            return $segments;
        }

        show_404($segments[0]);
    }
}

Credits.

Tpojka
  • 6,996
  • 2
  • 29
  • 39
  • thanks for your reply ! but my problem is not able route on host. i checked but still not working. – Ryan Tran Mar 24 '16 at 08:31
  • Something wrong here. $route['default_controller'] = 'home'; => CI will find home.php class in controllers folder (not have :D) because home.php in frontend sub folder Btw, any solution does not redirecting ? – Ryan Tran Mar 27 '16 at 13:20