Codeigniter 404 error is shown - php

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/';

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 static pages tutorial

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')) {

CI routing issue

i am currently learning CI and i have come to an issue that i cant seem to solve.
i set up my wamp server in a drive and inside the www(root) folder i have extracted the codeigniter files.
![example][1][1]: http://i.stack.imgur.com/7RKqG.png
then I created my php files for view/model and controller and set the default route in the config/routes.php
so now when I go to my browser and type localhost I get the post.php displayed without anyissue.
but I am unable to access any of the views from here. for example i have a new_post.php view and when i type in the address bar localhost/new_post.php i get a "Not Found
The requested URL /new_post.php was not found on this server." error.
what am i doing wrong? below i have posted the code which i have written in the post.php controller along with a image of the file structure/names i have.
posts.php - controller
<?php
class Posts extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('post'); //loads the post model u created in the models folder
}
function index() //goes to this function 1st when u access the controller
{
$data['posts']=$this->post->get_posts(); // load all the data from the get_posts function in post model to the data array posts
$this->load->view('post_index', $data); //loads the view
}
function post($postID)
{
$data['post']=$this->post->get_post($postID);
$this->load->view('post', $data);
}
function new_post()
{
if($_POST)
{
$data=array(
'title'=> $_POST['title'],
'post'=> $_POST['post'],
'active' =>1
);
$this->post->insert_post($data);
redirect(base_url(). 'posts/');
}
else
{
$this->load->view('new_post');
}
}
function editpost($postID)
{
$data['success']=0;
if($_POST)
{
$data_post=array(
'title'=> $_POST['title'],
'post'=> $_POST['post'],
'active' => 1
);
$this->post->update_post($postID,$data);
$data['success'] =1;
}
$data['post']=$this->post->get_post($postID);
$this->load->view('edit_post',$data);
}
function deletepost($postID)
{
$this->post->delete_post($postID);
redirect(base_url(). 'posts/');
}
}
![structure][1] [1]: http://i.stack.imgur.com/SnsbW.png
In CodeIgniter you have to use controller to get access to his function
you are saying "when i type in the address bar localhost/new_post.php i get a Not Found" because you try to direct access its function name you have to use example.com/controllername/functionname like this
http://localhost/posts/new_post
for more information check codeignier url
https://ellislab.com/codeigniter/user-guide/general/urls.html
if you not removed index.php using .htaccess then you have to use your url like this
http://localhost/index.php/posts/new_post
In CodeIgniter, everything runs through the main "index.php" file, in your root directory.
So, you would access your new post page, like this;
http://localhost/index.php/posts/new_post
Have a read through the CodeIgniter user guide, it will answer any problem you have.
https://ellislab.com/codeigniter/user-guide/

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