Code Igniter Function Call 404
Asked Answered
W

7

7

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.

Windsor answered 3/11, 2009 at 3:50 Comment(2)
Have you used any sort of Routing? or Mod_rewrite? Other than that, I can't seem to get how it isn't working. [1]: codeigniter.com/user_guide/general/routing.htmlBodycheck
This is almost certainly a routing issue. Make sure you don't have any catch-all routes from the previous steps in the user guide. If you have any catch-alls, it won't route properly to the controller.Equisetum
C
2

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

http://example.com/index.php?/blog/comments
Closestool answered 4/11, 2009 at 7:30 Comment(0)
R
2

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.

Raddie answered 27/11, 2012 at 23:11 Comment(0)
A
1

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

Argentinaargentine answered 3/11, 2009 at 5:5 Comment(0)
S
1

It's always worth trying out some of the $config['uri_protocol'] options in application/config/config.php.

/*
|--------------------------------------------------------------------------
| URI PROTOCOL
|--------------------------------------------------------------------------
|
| This item determines which server global should be used to retrieve the
| URI string.  The default setting of "AUTO" works for most servers.
| If your links do not seem to work, try one of the other delicious flavors:
|
| 'AUTO'            Default - auto detects
| 'PATH_INFO'       Uses the PATH_INFO
| 'QUERY_STRING'    Uses the QUERY_STRING
| 'REQUEST_URI'     Uses the REQUEST_URI
| 'ORIG_PATH_INFO'  Uses the ORIG_PATH_INFO
|
*/
$config['uri_protocol'] = 'AUTO';

Some servers have issues with different options, so try each manually. This may not work in your case but has saved the day for me in the past.

Stockroom answered 21/12, 2009 at 11:30 Comment(0)
W
0

I's a file update issue.

Windsor answered 4/11, 2009 at 3:29 Comment(0)
M
0

I had the same problem. Ended up being that I never closed one my first function - I left off the last }. So the function I didn't close worked fine but everything after that kept giving me a 404.

Malikamalin answered 29/11, 2009 at 1:32 Comment(0)
G
0

In some versions of CodeIgniter your controller name(file name) must be start with capital letter

eg..

 Blog.php 

if the first letter of the file is not capital then it may show 404 error.

Gone answered 25/4, 2016 at 19:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.