2
votes

I've recently downloaded CodeIgniter and install it in XAMPP htdoc -> codeigniter(application,system,user guide,index.php,license.txt)

I've tried to follow with the tutorial posted in CodeIgniter's User Guide on creating static pages but I keep getting this error:

Unable to load the requested file: pages/home.php

The controller is at application/controllers/pages.php with the following code:

class Pages extends CI_Controller 
{

    public function view($page = 'home')
    {
        if(file_exists('application/views/pages/'.$page.'.php'))
        {
            //whoops we don't have a page for that!
            show_404();
        }

        $data['title'] = ucfirst($page); //Capitalize the first letter
        $this->load->view('templates/header', $data);
        $this->load->view('pages/'.$page, $data);
        $this->load->view('templates/footer', $data);
    }
}

The tutorial also mentioned about creating header.php in application/views/templates/header.php and footer.php in application/views/templates/footer.php, which are just html code and echo out the page title.

And finally also creating a home.php and about.php in application/views/pages/ directory. For home.php and about.php I only type out some plain text.

Where did I go wrong. Any advice would be greatly appreciated. :) Thank you.

4
How are you accessing this page? try accessing domain.com/index.php/pages/home.php.rgin
It bring me back to the XAMPP page or just localhost.Jamie
I just installed CodeIgniter, am going through their "easy" static page demo, and mine didn't work. You have to type so many things that it is easy to get a character or two wrong and have it not work. This pointed me go through my Pages->view class and find my char errors.Andrew Koper

4 Answers

3
votes

Try changing this line:

if(file_exists('application/views/pages/'.$page.'.php'))

to this:

if(!file_exists( APPPATH . 'views/pages/'.$page.'.php'))
1
votes

can you change this line with

if(file_exists('application/views/pages/'.$page.'.php'))

something like

if(file_exists(dirname(dirname(__FILE__)).'/views/pages/'.$page.'.php'))

and let me know if that works.

1
votes

In the static page tutorial you are asked to:

Create a file at application/controllers/pages.php with the following code.

But this won't work since the file name needs to start with a capital like this:

application/controllers/Pages.php

This did the trick for me

0
votes

Whether the format is mysite.com/index.php/home or mysite.com/home doesn't matter because it's looking for the view, not the controller.

if(file_exists('application/views/pages/'.$page.'.php'))

should be

if(!file_exists('application/views/pages/'.$page.'.php'))

That isn't what's causing your problem, though. You're showing a 404 when it evaluates to true, which means that the file actually does not exist. If the file existed, you would get a 404 page.

Check thoroughly and make sure you have the file 'application/views/pages/home.php'.