Reputation: 63
I'm new to CI, I barely managed to start my own website and had it running for a while now, I had all the views structure as the CI guide, but then I want to put a php file called igniel.php inside a folder called "vcard" inside the pages folder.
The problem is, when I try to load it using a full path like :
http://example.com/application/views/pages/vcard/igniel.php
its showing up nicely, but once I use
http://example.com/igniel (without the php)
it will show a 404 error.
here is my Pages.php content :
<?php
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');
$this->load->view('templates/nav');
$this->load->view('pages/'.$page);
$this->load->view('templates/footer');
}
}
and here is my routes.php content :
$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1/$2';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
I've tried to googling and use a few suggestion around but still no luck...
many thanks in advance.
Best,
Upvotes: 0
Views: 1129
Reputation: 7111
You are not sopposed to call view file. Instead you should make request through controllerwhich is meant to call specific file. So you never want to call (nor I am sure how you succeeded in request with that URL):
http://example.com/application/views/pages/vcard/igniel.php
but
http://example.com/pages/view/igniel
(withouth the extension).
You should follow basic example from CI documentation. In your case it means you are trying to use subdirectory vcard with same code in controller that doesn't fit your needs.
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();
}
elseif ( $page == 'igniel' )// your page is in subdirectory
{
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('templates/header');
$this->load->view('templates/nav');
$this->load->view('pages/vcard/'.$page);
$this->load->view('templates/footer');
}
else
{
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('templates/header');
$this->load->view('templates/nav');
$this->load->view('pages/'.$page);
$this->load->view('templates/footer');
}
}
}
But, if you want to call it like:
http://example.com/igniel
You have to change line in controller to include that file within it's path as described in elseif
line.
Routing should take place like:
$route['(:any)'] = 'pages/view/$1';
If you have more subdirectories, you have to fit code in controller to aproach those as I describe it here for you.
Upvotes: 1