4
votes

I started to try the first tutorial example on http://ellislab.com/codeigniter%20/user-guide/tutorial/static_pages.html.

I always get the Page Not Found Problem! I have read a lot of articles about to solve the problem. But it doesn't matter how often I try the solutions in different forums, it's not working!

Can someone please let me know where my bugs are?

So these are some files I tried:

My routes.php in application/config

$route['default_controller'] = "pages/view";
$route['404_override'] = '';
$route['(:any)'] = 'pages/view/$1';

My pages.php in application/controllers

<?php
   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);
    }
}

My home.php and about.php are on application/views/pages/ and the header.php and the footer.php are on application/views/templates/

Thank you very much!

6
Can you insert var_dump($page); return; before if(!file_.....? This way we check if this method starts. - Paul
Can you write what URL do you trying to open? - Paul
What if you just take out that if(!file_exists) and let CI handle the missing view how it already does anyway, and see what happens? - Damien Pirsy
Ok now it works but this is very strange because the solution was to just try if it would work with wampp instead of xampp. I can not really say what was wrong but thank you very very much guys for your help!!! - user3173028

6 Answers

0
votes

Take the file exists or not condition out,it will work,because what you are checking is not a file,but just an URL to route to. So obviosuly it will always return false,which will lead to failure of your work.

0
votes

In application/config/routes.php

Try changing

$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';

To

$route['default_controller'] = 'pages/views';
$route['404_override'] = '';

Hope it works. Worked for me though.

0
votes

Try changing the code in /application/config/routes.php to:

$route['default_controller'] = "pages/view";
$route['(:any)'] = 'pages/view/$1';
$route['404_override'] = '';

CodeIgniter reads the code line by line, so its choosing to display the 404 page before the page you created.

0
votes

Make sure to follow the directions closely. I was having the same problem and searched for a solution. Only after reading some of these answers did I realize that I hadn't created the templates or pages sub-directories within the views directory. By the time you get to the end of the static pages tutorial, you should have a structure as follows:

/views/
   /templates/
     header.php
     footer.php
   /pages/
     about.php
     home.php
0
votes

Firstly run this

$route['default_controller'] = 'welcome';
$route['404_override'] = '';

Then try this

$route['default_controller'] = "pages/view";
$route['404_override'] = '';

I had the same problem but not it solved.

0
votes

Create a .htaccess file in your project’s root directory and add the following code

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]