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/tutorial/static_pages.html
Here's their Example of Controller class Pages:
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);".
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.
No comments:
Post a Comment