codeigniter static pages tutorial - php

I'm getting into frameworks and I'm using CI so i can more familiar with OOP. In the static pages tutorial in the pages controller I am told to copy this code
<?php
class Pages extends CI_Controller {
public function view($page = 'home') {
if (!file_exists(APPPATH . '/views/pages' . $page . '.php')) {
// we dont have a page for that!
show_404();
}
$data['title'] = ucfirst($page); //Capitilize the first letter
$this->load->view('templates/header', $data);
$this->load->view('pages/' . $page, $data);
$this->load->view('templates/footer', $data);
}
}
?>
in the if statement its blocking the pages I'm trying to load such as about.php and index.php
when I remove the ! in the if statement I'm able to render the about.php and index.php which is good, I'm able to load my pages according to the tutorial, but if I type in something random like asdasd.php i don't get the 404 error I get an AN ERROR WAS ENCOUNTERED Unable to load the requested file: pages/asdasd.php
How can i go about fixing this issue, having CI display the 404 when the file isn't there.
Thanks

It seems the issue is in the path in your file_exists call.
You need to add a / (slash) just after /pages in the following line:
if (!file_exists(APPPATH . '/views/pages' . $page . '.php')) {

Related

CodeIgniter - Loading View inside subfolder showing 404

Trying to load a view from a subfolder the path is like:
(views/pages/users/test.php)
Below is my Pages.php file
<?php
class Pages extends CI_Controller{
public function view($page = '/') {
if(!file_exists(APPPATH.'views/pages/'.$page.'.php')) {
show_404();
}
$data['title'] = ucfirst($page);
$this->load->view('templates/header');
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer');
}
}
I already tried with controller and changed my code but still I am not able to view the page test.php which is inside the folder view/pages/users/test.php
PFB tried code:-
public function subview($page = '/') {
if(!file_exists(APPPATH.'views/pages/users'.$page.'.php')) {
show_404();
}
$data['title'] = ucfirst($page);
$this->load->view('templates/header');
$this->load->view('pages/users'.$page, $data);
$this->load->view('templates/footer');
}
Getting error message:-
404 Page Not Found The page you requested was not found.
First you will need to fix your if statement at the top to reflect the correct path:
if(!file_exists(APPPATH.'views/pages/users/'.$page.'.php')) {
show_404();
}
And then fix the load call like so:
$this->load->view('pages/users/'.$page, $data);
The problem is, you didn't add the / after the users in your path so the file can't be found and will always show 404 because of it.
I just set up a test project to demonstrate this code:
Then in the Welcome.php controller I created a testSubView function which looks like this:
function testSubView($page = ""){
if(!file_exists(APPPATH. "views/pages/users/{$page}.php")) {
show_404();
}
echo "The file {$page}.php exists<br/>";
echo "Loading the file pages/users/{$page}.php ...<br/><br/>";
$this->load->view("pages/users/$page");
}
Test.php file contents:
<?php
echo "This is content from test.php file";
So when I visit my apps url: localhost/app/index.php/welcome/testSubView/test
I will get this output:
The file test.php exists
Loading the file pages/users/test.php ...
This is content from test.php file
To explain the demo url a bit:
welcome - Is the name of a controller file
testSubView - Is the name of a function inside of the controller file
test - Is the name of the view file I'm loading inside of the function

A folder inside views folder, Routing and/or Controller error in CI?

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,
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.

Codeigniter 404 error is shown

I am following the tutorial-Static pages, from codeigniter website.
I extracted the codeignitor zip into my local server.
Edited the line
$config['base_url']='http://localhost/';
in application /config/config.php
Created the file: application/controllers/pages.php to have following contents:
<?php
class Pages extends CI_Controller {
public function view($page = 'home')
{
if ( ! file_exists('/var/www/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);
}
}
Created the file: application/views/templates/header.php with some HTML content.
Created the file: application/views/templates/footer.php with some HTML content.
Now, when I go to
localhost/index.php/pages/view/about
I expect the 'about' page, but 404 error is shown.
Why is that?
please try following
if (file_exists(APPPATH."views/pages/". $page. ".php")) {
$this->load->view("pages/". $page);
}
APPPATH = well self explanatory
I think the problem is here-
if ( ! file_exists('/var/www/application/views/pages/'.$page.'.php'))
You should also mention the name of your codeigniter folder as well!
Some webservers expect a question mark, like so:
localhost/index.php?/pages/view/about
Can you test this?
Instead of this
$config['base_url']='http://localhost/';
Try
$config['base_url']='http://localhost/project_name/';

Codeigniter Setting Homepage ( Default Controller )

I'm trying to implement page templating in my codeigniter application, templating is working fine for instance, i have a blog page, which i'm trying to assign as my homepage, with different view and pagination and so on. When i write www.mywebsite.com/blog, it gets opened in homepage template, or assume that i have a page www.mywebsite.com/about , it gets opened in page template too. But when i try to access my website via www.mywebsite.com, i have a 404 error page. How can i assign my blog page as the homepage ?
Page Controller :
class Page extends Frontend_Controller {
public function __construct(){
parent::__construct();
$this->load->model('page_m');
}
public function index() {
// Fetch the page template
$this->data['page'] = $this->page_m->get_by(array('slug' => (string) $this->uri->segment(1)), TRUE);
count($this->data['page']) || show_404(current_url());
add_meta_title($this->data['page']->title);
add_meta_keyword($this->data['page']->keywords);
add_meta_description($this->data['page']->description);
// Fetch the page data
$method = '_' . $this->data['page']->template;
if (method_exists($this, $method)) {
$this->$method();
}
else {
log_message('error', 'Could not load template ' . $method .' in file ' . __FILE__ . ' at line ' . __LINE__);
}
// Load the view
$this->data['subview'] = $this->data['page']->template;
$this->load->view('_main_layout', $this->data);
}
private function _page(){ // methods }
private function _homepage(){ // methods }
}
in my Routes, i have set my default controller to page controller
$route['default_controller'] = "page";
application/config/routes.php
$route['default_controller'] = 'namecontroller';
The problem is that there's no URI segment when you visit www.mywebsite.com. You can try setting the default value as:
$this->uri->segment(1 , 'blog')
Your code
// Fetch the page template
$this->data['page'] = $this->page_m->get_by(array('slug' => (string) $this->uri->segment(1)), TRUE);
count($this->data['page']) || show_404(current_url());
the uri segment code
$this->uri->segment(1)
is looking first url segment, when you browse your site like www.yousite.come/blog it will work find but when you do www.yoursite.com 1st uri segment is missing so it will return false , so i will show 404 page.
Solution :
You can just add the second parameter to the function like
$this->uri->segment(1,'blog');
now if the first url segment is missing it will not return false,it will return the default vlaue 'blog'
For more information about this you can see codeingitor documentation

CodeIgniter language code routing

I would like to add a language code segment to my URIs in CodeIgniter, but I'd like to somehow — possibly with routing — force the browser to stop interpreting a language code as a path.
I haven't yet successfully implemented any of the i18n libraries in my CodeIgniter install, and I'm fairly certain my problem is simple enough to solve without a library anyway.
The method I had in mind was simply to load the appropriate language files respective of the language code that appears in the URI.
For example
http://example.com/about // Default language
http://example.com/sv/about // Load Swedish language
Here's the controller logic:
<?php
// ** Update **
// The following updated code works, as long as the language code appears
// at the end of the URI, e.g, http://example.com/about/sv
//
// Ideally, I would like the language code segment to always appear first.
//
// There is also the problem of keeping the language selected while
// navigating around the site…
class Pages extends CI_Controller {
public function view ($page = 'home') {
if (!file_exists('application/views/pages/'.$page.'.php'))
show_404();
$uri = explode("/", $_SERVER['REQUEST_URI']);
$lang_code = end($uri);
$data['title'] = $lang_code;
switch ($lang_code) {
case "sv": $the_language = "swedish"; break;
case "no": $the_language = "norwegian"; break;
default: $the_language = "english";
}
$this->lang->load('general',$the_language);
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
?>
Am I going about this the wrong way? If so, why? Please avoid canned responses.
I would go with CI routing:
$route['([a-z]+)/([a-z]+)'] = "pages/view/$2";
Then, your controller would look something like this:
<?php if (! defined('BASEPATH')) exit('No direct script access');
class Pages extends CI_Controller {
function __construct() {
parent::__construct();
$this->language = $this->uri->segment(1);
}
function view($page = 'home')
{
/* other stuff */
$this->lang->load('general',$this->language);
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}

Categories