0
votes

I am asked to study about CodeIgniter but I haven't even heard about it before, so I'm trying to go through the textbook and official tutorial. But I can't even successfully display a Hellow World...I've tried to search the solution for a while but none of them helps.

Here's the specific problem. I'm building a CodeIgniter environment with MAMP, PHP version is 5.6.2 and CodeIgniter version is 2.2.0. But no matter what I do, the site can only show me the default controller or 404 page.

First of all here's "hello.php" in controller folder:

<?php
class Blog extends CI_Controller {

    public function index()
    {
        echo 'Hello World!';
    }
}
?>

I haven't set the mod_rewrite yet so I simply goes to this address: localhost:8888/CodeIgniter/index.php/hello

And I only get a 404 page. So I tried to modify uri_protocol in config.php. I tried all of them, and QUERY_STRING and ORIG_PATH_INFO worked fine at first. I thought I succeeded. But I was wrong. I then setup a mod_rewrite with .htaccess file and I can only get a default controller (which is welcome page). I thought I must did something wrong in my .htaccess file so I tried to edit it but it doesn't work, so I tried to remove the file and go back to index.php/hello again, then I found that it can't work anymore, what I get is only a default welcome page.

I then tried to restart the server, modify index_page in config.php to 'index.php?', move CodeIgniter to root directory (I mean instead of localhost:8888/CodeIgniter/index.php, I removed CodeIgniter folder and just go to localhost:8888/index.php), totally remove the current CodeIgniter folder and download it again from official site, and of course tried other choices of uri_protocol. But nothing helps. What I get is still only default controller or a 404 page.

In summary, in config.php file, if I set

$config['index_page'] = 'index.php';

then

$config['uri_protocol'] = 'QUERY_STRING';
$config['uri_protocol'] = 'ORIG_PATH_INFO';

returns the default controller (even if I try to access a page which actually doesn't exist in controller folder), and other uri_protocols return 404 page.

Else, if I set

$config['index_page'] = 'index.php?';

then

$config['uri_protocol'] = 'PATH_INFO';
$config['uri_protocol'] = 'ORIG_PATH_INFO';

returns the default controller (also even if I try to access a page which doesn't exist in controller folder), and others return 404 page.

Also I thought it may be a problem with MAMP but even if I upload the whole site onto a VPS server (CentOS 6.5, php version 5.3.3) the totally same problem occurs.

Anybody has some idea?

2

2 Answers

0
votes

First of all, to remove the index.php from your url, create a .htaccess file in your site root, and add the following code:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]  

and then in your config/config.php file :

$config['base_url'] = '';
$config['index_page'] = '';

set the rest config parameters as usual.

Now, you are using the url localhost:8888/CodeIgniter/index.php/hello to access your application. This is wrong (unless you are routing the url). You have to load your controller which in turn loads the view for you. So, now use:

localhost:8888/CodeIgniter/blog

Again. One more thing. In the constructor of your Controller, you will have to call the parent constructor so that you can use loader class to load librariee, view, models whatever.

Now lastly, your best bet is to go through the documentation well. You have tried but not tried enough! Good Luck.

0
votes

Umm well thanks for your help! A friend on Twitter told me that the class name was wrong, it should be the same as the php file (in this case, it should be class Hello extends CI_Controller)

After that, all things goes OK!