I was going through the official Code Igniter tutorial when I hit a snag...
The tutorial had me save and run this code:
<?php
class Blog extends Controller {
function index()
{
echo 'Hello World!';
}
function comments()
{
echo 'Look at this!';
}
}
?>
IF I enter the following URL:
index.php/blog
it works and displays "Hello World!".
When I modify the URL to display the comments as follows:
index.php/blog/comments/
I get a 404.
if you add a ? after index.php does it work?
http://example.com/index.php?/blog/comments
I came across this old post without a good answer as to why it was happening. I too came across the same apparent error that you did and was struggling to fix it. I realized the problem came from the routing that was set in earlier CI examples. My page wasn't working at all unless I added the following line inside config/routes.php:
$['blog'] = 'blog';
That is because of this line that considers anything, other than what you had already set, as arguments for the root:
$route['(:any)'] = 'pages/view/$1';
If you remove the above line, it'll all work, except the root won't work anymore as it did in the previous tutorials. I had to also add the following line so that we can call functions inside the controller:
$route['blog/(:any)'] = 'blog/$1';
With both of these two added, you can call functions on the root and yet also have a working "blog" controller.
By default, your example should work. Examine your configurations and remove .htaccess as your example aren't using mod_rewrite.
Start from scratch also helps you learn ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With