Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter Documentation Example Code Error | Explained and Fixed
#1

CodeIgniter Documentation sample code error. 404 Page Not Found The page you requested was not found.

First of all go have a look at CodeIgniter Static pages documentation.
http://www.codeigniter.com/user_guide/tu...pages.html

Here's their Example of Controller class Pages:
Quote:class Pages extends CI_Controller {
   public function view($page = 'home')       {
          if ( ! file_exists(APPPATH.'/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);
   }
}

Now as you can see the 'APPPATH' CONSTANT on line 3 is path to the 'application' folder and it's defined in our main index.php file.

If you go and create template folder, put files in, same with pages and whatnot and run it, it wont run. It'll instead invoke show_404() function saying page doesn't exist. Specifically tell us, "APPPATH.'/views/pages/'.$page.'.php'" doesn't exist. It's really a pain because it does indeed exist even after setting up everything correctly and granting full perms.

So after searching in Google for about 4-5 hours and trying to resolve it myself I decided to check the exact value of that 'APPPATH' CONSTANT. That's how it's defined: "define('APPPATH', $application_folder.DIRECTORY_SEPARATOR);".

Quote:if ( ! is_dir(BASEPATH.$application_folder.DIRECTORY_SEPARATOR))
{
    header('HTTP/1.1 503 Service Unavailable.', TRUE, 503);
    echo 'Your application folder path does not appear to b$
    exit(3); // EXIT_CONFIG
}


It seem perfectly fine at first glance but what's that 'DIRECTORY_SEPERATOR' CONSTANT doing there? well I guess it's just adding a directory separator '/'. Perfectly fine, right? Now if you've been following their documentation they've a Directory separator '/' before 'views' aswell, '! file_exists(APPPATH.'/views/pages/'.$page.'.php'', which means APPPATH will add one directory separator, and if you follow the documentation they've added another directory separator in total two separators, making file_exist function return false and thence error message!!

This is what we're basically asking file_exists() function to check. Does /var/www/application/.'/views/pages/'.$page.'.php'. exist? It'll always return false due to 2 slashes! To fix the issue you need to remove the clash before views. - See more at: http://www.webdevtown.com/2016/02/codeig...Qlr0w.dpuf
Reply
#2

You appear to be correct Undecided
Will you be submitting a PR to fix this?
James Parry
Project Lead
Reply
#3

It would probably be more appropriate to use the VIEWPATH constant:

PHP Code:
class Pages extends CI_Controller 
 
   public function view($page 'home')
    {
        if ( ! 
file_exists(VIEWPATH.'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);
    }

Reply
#4

The slash may be unnecessary, but the example is not in fact erroneous - multiple slashes should be and are in fact automatically reduced to a single one by PHP.

Quote:php > mkdir(__DIR__.DIRECTORY_SEPARATOR.'foo');
php > touch(__DIR__.DIRECTORY_SEPARATOR.'foo'.DIRECTORY_SEPARATOR.'bar');
php > var_dump(file_exists(__DIR__.DIRECTORY_SEPARATOR.'/////////////foo//////////////////////////////bar'));
bool(true)

So while I don't mind adjusting the docs, I'm more interested in the specifics of your environment, so we'd know why this is happening.
Reply
#5

(02-17-2016, 02:40 PM)Narf Wrote: The slash may be unnecessary, but the example is not in fact erroneous - multiple slashes should be and are in fact automatically reduced to a single one by PHP.

Quote:php > mkdir(__DIR__.DIRECTORY_SEPARATOR.'foo');
php > touch(__DIR__.DIRECTORY_SEPARATOR.'foo'.DIRECTORY_SEPARATOR.'bar');
php > var_dump(file_exists(__DIR__.DIRECTORY_SEPARATOR.'/////////////foo//////////////////////////////bar'));
bool(true)

So while I don't mind adjusting the docs, I'm more interested in the specifics of your environment, so we'd know why this is happening.

Can't believe that you're actually right. Now I'm trouble replicating the issue.. dam. Confused
Reply




Theme © iAndrew 2016 - Forum software by © MyBB