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 ;)
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.
I's a file update issue.
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.
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.
精彩评论