CodeIgniter Error opening Home Page - php

I create a Home Page and when I want to access it's written Page Not Founded
I create in my rount.php access but again there is same error
$route['pages/index']= 'pages/home';
Do I need to change my controller or model or just route.php file
Pages controller
<?php
class Pages extends CI_Controller{
public function index(){
$this->load->view('pages/home');
}
public function view($page='home'){
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');
}
}

try like this . and make sure you are linking right base_url for home index function like this. follow this if its help .
echo base_url("pages/home");
$route['pages/home']= 'pages/index';

Related

controller for view not working codeigniter

I have two views ie, views/pages/home.php and views/pages/search_result.php. I have a controller to load this views ie, controllers/Pages.php. And also i have one more folder inside view ie, views/templates/header.php and views/templates/footer.php
When i am pointing the browser to http://localhost/codeigniter/home everything working perfect.
But the problem is when i am pointing the browser to http://localhost/codeigniter/search_result, the view footer.php is also showing. Actually am not given anything inside search_result.php
My controller code is,
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Pages extends CI_Controller {
public function home($page = 'home')
{
//code to show home.php (http://localhost/codeigniter/home)
if (!file_exists(APPPATH.'/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
else
{
$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);
}
}
public function search_result($page = 'search_result') {
//code to show search_result.php (http://localhost/codeigniter/search_result)
}
}
Inside search_result function i didn't given any code and while am pointing to http://localhost/codeigniter/search_result the footer from function home is showing ie, $this->load->view('templates/footer', $data);
What am doing wrong. Is there any solution to solve this issue. I am beginner in codeigniter.
Just simply try this
public function home()
{
//code to show home.php (http://localhost/codeigniter/home)
if (!file_exists(APPPATH.'/views/pages/home.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
else
{
$this->load->view('templates/header', $data);
$this->load->view('pages/Home', $data); # changed
$this->load->view('templates/footer', $data);
}
}

Codeigniter redirect URL issue

I am new to codeigniter. I have just made a login feature in CI with the help of google, but here I am redirecting to URL on login but It is not working.
Here is the details
after redirecting the link is like this http://localhost/xpensepedia/index.php/home
which is giving the 404 error
404 Page Not Found
The page you requested was not found.
and my controller is
public function index() {
$this->load->view('header');
if (($this->session->userdata('user_id') != "")) {
redirect(site_url('home'));
} else {
$this->load->view("register");
}
$this->load->view('footer');
}
My files are in the image
Kindly check name of class you given to home.php Controller
Try this...
public function index() {
if (($this->session->userdata('user_id') != "")) {
redirect(site_url('home'));
} else {
$this->load->view('header');
$this->load->view("register");
$this->load->view('footer');
}
}
Home Controller :
class Home extends CI_Controller {
Public function index {
$this->load->view('header');
$this->load->view("home");
$this->load->view('footer');
}
}
you can add url path on constraints ....
you define the url like this on constants :
define('ROUTE_STIE_PATH','localhost/xxx');
define('ROUTE_STIE_PATH_LOGIN',ROUTE_STIE_PATH.'login/');
define('ROUTE_STIE_PATH_HOME',ROUTE_STIE_PATH.'home/');
and then you can echo by simply
echo ROUTE_STIE_PATH_LOGIN
if you want to call any methods with in home then
echo ROUTE_STIE_PATH_HOME.'methodname';
I solve similar case when I change my config file located in application>config>config.php to the following value:
$config['base_url'] = '';
$config['uri_protocol'] = 'REQUEST_URI';

How to access a controller method from view in CodeIgniter?

I want to do something like this in CodeIgniter:
Controller:
class Home extends CI_Controller {
public function index() {
$this->load->view('home');
}
public function user($user) {
return $user;
}
}
View (home):
Hi <?php echo user('User') ?> // call a controller method
How can i do this?
One way to get it is , returning it from the routes.php ,
Like $route['user'] = "home/user"; And this will execute the user method from the home controller.

page not found error in codeigniter

I have anchor in header.php as below:
Backend
Also have the controller named backend.php in Controllers folder.
I also routed this code in routes.php file like
$route('default_controller')='frontend';
$route('backend')='backend';
Backend controller page is:
<?php
class Backend extends CI_Controller {
public function __construct(){
parent::__construct();
}
public function index(){
$data['title'] = 'Backend Page';
$this->load->view('templates/header', $data);
$this->load->view('backend/reg', $data);
$this->load->view('templates/footer');
}
}
Although getting error 404 Page not found.
Make sure all controller and models etc Ucfirst example Frontend.php instead of frontend.php
Codeigniter Docs http://www.codeigniter.com/docs
Auto load url helper in application.config/autoload.php
Change route from
$route('default_controller') ='frontend';
to
$route['default_controller'] = "frontend";
$route['backend'] = "backend";
I you have not set up you config to remove index.php with htaccess then use index.php in link.
Backend
Example controllers/Backend.php
<?php
class Backend extends CI_Controller {
public function __construct(){
parent::__construct();
}
public function index(){
$data['title'] = 'Backend Page';
$this->load->view('templates/header', $data);
$this->load->view('backend/reg', $data);
$this->load->view('templates/footer');
}
}
Example controllers/Frontend.php
<?php
class Frontend extends CI_Controller {
public function __construct(){
parent::__construct();
}
public function index(){
$data['title'] = 'Frontend Page';
$this->load->view('templates/header', $data);
$this->load->view('frontend/reg', $data);
$this->load->view('templates/footer');
}
}
Link here for htaccess examples for removing index.php on windows os https://github.com/riwakawebsitedesigns/htaccess_for_codeigniter
Try this one :
Backend // include also the folder where your backend file is stored
I hope this helps.

Error when trying to load view in my_controller

Consider my code:
<?php
class MY_Controller extends Controller {
public function __construct()
{
parent::Controller();
}
function _displayPage($page, $data = array()) {
$this->load->view('structure/header', $data);
$this->load->view($page, $data);
$this->load->view('structure/footer', $data);
}
}
?>
page.php
<?php
class Page extends MY_Controller {
function __construct() {
parent::__construct();
}
function index() {
$data['content'] = array('title'=>'hello world');
$this->_displayPage('home', $data);
}
}
?>
Upon loading my page on my browser, I get this error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Page::$view
Filename: libraries/MY_Controller.php
Line Number: 11
Does anyone know what I'm doing wrong?
Thanks
In your library My_Controller you should be using the parent keyword instead of $this.
your code should look like so:
class MY_Controller extends Controller
{
public function __construct()
{
parent::Controller();
}
function _displayPage($page, $data = array())
{
parent::load->view('structure/header', $data);
parent::load->view($page, $data);
parent::load->view('structure/footer', $data);
}
}
If I understand what you're trying to accomplish correctly, you're wanting to setup a template that includes your header view and footer view, but without calling header and footer views for each controller you use throughout your application. Here's what I've done to accomplish this.
First, create a new folder under your views, for this example we'll call it 'includes'. Inside the newly created includes folder, create three files, header.php, footer.php and template.php. Setup your header and footer appropriately and then edit your template.php to look as follows:
<?php echo $this->load->view('includes/univ_header'); ?>
<?php echo $this->load->view($main_content); ?>
<?php echo $this->load->view('includes/univ_footer'); ?>
Now, from your controller you can define what view you would like to set as your 'main_content'. For example, if you have home.php in your views folder and you want to wrap it with your header and footer you would do so in your controller as follows:
function index() {
$data['content'] = array('title'=>'hello world');
$data['main_content'] = 'home';
$this->load->view('includes/template', $data);
}
Hope this helps!

Categories