I have been asked to look into an issue where the default_controller in a CodeIgniter 3 project does not seem to be called. Instead we are seeing a 404 error.
In the application/controllers
folder there is a Welcome.php
file with the following content:
class Welcome extends CI_Controller {
public function __construct()
{
parent::__construct();
// Your own constructor code
}
public function index()
{
print('hello');
$this->load->view('welcome_message');
}
}
The application/config/routes.php
file has:
$route['default_controller'] = "welcome";
I only see a 404 and none of the expected text.
Adding a print
statement to the routes.php
shows that it is being loaded. Also, tying it explicitly to a route has it called, but not when it is set to be the default controller.
$route['blah'] = "welcome"
Can anyone suggest what may be going on?
BTW We are using PHP7 on an Ubuntu 16.04 machine.
Turns out the project was an upgrade from a CodeIgniter 2 project and there were some migration steps that hadn't quite been completed. It turned out that there was a MY_Router.php in the libraries folder, that seemed to be throwing things off - at least moving it into applications/core solved the issue.
An additional modification was needed in the MY_Router.php file given the capitalization of the filenames, requested as part of the CI3 migration:
function _validate_request($segments)
{
// Does the requested controller exist in the root folder?
if (file_exists(APPPATH.'controllers/'.ucfirst($segments[0]).'.php'))
{
return $segments;
}
...
}
BTW I was sure it wasn't a HTTP server rewrite issue, since a CodeIgniter 404 error page was showing the expected call path. I had added a print(_SERVER['REQUEST_URI'])
to see what was going on.
Edit: I now see that ucfirst approach above is not ideal in other installations, but given that there are no subfolders in this project's controllers folder, it is a quick fix for this project.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.