Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code Igniter Function Call 404

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.

like image 684
Jarrod Avatar asked Nov 03 '09 03:11

Jarrod


3 Answers

if you add a ? after index.php does it work?

http://example.com/index.php?/blog/comments
like image 191
gaker Avatar answered Nov 13 '22 23:11

gaker


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.

like image 36
zeMinimalist Avatar answered Nov 13 '22 22:11

zeMinimalist


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 ;)

like image 1
Trav L Avatar answered Nov 13 '22 22:11

Trav L