Why is my CodeIgniter website loading from bottom to top? - php

Default Controller is HomePage.php
$route['default_controller'] = 'index/home_page';
Index.php
public function home_page()
{
$data['subview']='subview/home_page';
$this->load->view('_layout_home.php',$data);
}
_layout_home.php
<?php $this->load->view('include/header_home')?>
<?php $this->load->view($subview)?>
<?php $this->load->view('include/footer')?>
There is no autofocus attribute used in HTML or CSS.
The above code is for a home page of CodeIgniter web page which loads from bottom to top.

First letter only must be upper case on class and filename
https://www.codeigniter.com/user_guide/general/styleguide.html#file-naming
Change filename from
HomePage.php // Wrong Way
To
Homepage.php // Correct Way
Controller
https://www.codeigniter.com/user_guide/general/styleguide.html#class-and-method-naming
https://www.codeigniter.com/user_guide/general/views.html#loading-multiple-views
Path application > controllers > Homepage.php
https://www.codeigniter.com/user_guide/general/controllers.html
<?php
class Homepage extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
}
public function home_page() {
$data['subview']='subview/home_page';
// No need for .php
$this->load->view('layout_home',$data);
}
}
Then on routes.php
$route['default_controller'] = 'homepage/home_page';
https://www.codeigniter.com/user_guide/general/routing.html#examples
View
filename layout_home.php
<?php $this->load->view('include/header_home')?>
<?php $this->load->view($subview)?>
<?php $this->load->view('include/footer')?>

Related

When I try to echo my header code, the php code to do so is rendered as plain text

The PHP code at the top of my view page is as follows:
<?php
echo $this->view('includes/header');
?>
$this->view(); is a function inside my main Controller class. The Home controller class that is rendering the view extends that controller class.
Main Controller Class:
<?php
/*
* main controller class
*/
class Controller
{
public function view($view, $data = array())
{
extract($data);
// code...
if(file_exists("../private/views/". $view . ".view.php"))
{
return file_get_contents("../private/views/". $view . ".view.php");
} else {
return file_get_contents("../private/views/404.view.php");
}
}
}
Home Controller Class:
<?php
/**
* home controller
*/
class Home extends Controller
{
public function index()
{
// code...
echo $this->view('home');
}
}
When I view my page, all I see is:
Instead of seeing my page with the header files.
Thanks in advance for any help!
Sorry for anything I don't understand. I am relatively new to PHP and MVC.
What you want to do in your view method is return require "paths/there" . If you return the result of file_get_content(), you get the file as plaintext, so it isn't parsed as a php file. You may want to use require, require_once, include or include_once following your needs.
Are you sure the block of code below is called after initialization of class with method view()?
<?php
echo $this->view('includes/header');
?>
It seems $this doesn't have any methods.
So the problem of your code is here:
require_once('*Your controller url file*');
$Controller = new Controller();
echo $Controller->view('includes/header');
This mode you search to the controller file and instantiate the object.
Functions is functionally, but export of the controller is wrong.

How to create custom 404page

I want to create custom 404 page for my site.I mean when i type url as
http://example.com/anydummytext
need to redirect to custom 404page
How to create custom page for this
The simple way is create a controller you can name the error controller to any what you want.
Example
<?php
class Not_found extends CI_Controller {
public function index() {
// some content
$this->load->view('not_found');
}
}
Then on another controller you can redirect it
redirect('not_found');
Example Only
<?php
class Home extends CI_Controller {
public function index() {
$result = $this->some_model->get();
if ($result) {
// content
$this->load->view('home');
} else {
redirect('not_found');
}
}
}
The other option is in config/routes.php you can use codeigniter
$route['404_override'] = 'not_found'; // Note will not work in a subfolder
You can create a new controller or use existing controller to write down a function for loading the VIEW for 404.
class Nopage extends CI_Controller
{
/* Index function
*
* return void
*/
public function index()
{
$this->load->view('nopage_404');
}
}
}
Call the controller function in config->routes as below.
$route['404_override'] = 'nopage';

404 file not found in codeigniter

I am new to CodeIgniter MVC. My home view is loaded correctly. When i replace home in index() with aboutus it is working, but when i call aboutus function then it is showing 404 file not found error. Login is also working correctly. I am not getting what is wrong. My view folder contains aboutus, home and login files.
public function index()
{
$this->load->view("home");
}
function login()
{
$this->load->view("login");
}
function aboutus()
{
$this->load->view("aboutus");
}
//Here the html code
<li>Login</li>
<li><a href="<?php echo base_url();?>index.php/aboutus" >About Us</a></li>
How to load default controller
In config/routes.php
$route['default_controller'] = "controller_name; //this load index() in provided controller
$route['default_controller'] = "controller_name/method_name"; //this load method which you created inside provided controller(ex: main/about_us)
How to create controller
Path - application/controllers/
File name - main.php
inside main.php
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Main extends CI_Controller {
public function index()
{
$this->load->view("home");
}
function login()
{
$this->load->view("login");
}
function aboutus()
{
$this->load->view("aboutus");
}
}
How to create view
Path - `application/view/`
File name - `home.php`
File name - `login.php`
File name - `aboutus.php`
How to use Link
Home
Login
aboutus
How to use base_url()
in config/autoload.php
$autoload['helper'] = array('url');
in config/config.php
$config['base_url'] = '';
Change your html like this
home
about us
You need to add route to your new function aboutus() in routes.php
You can find routes.php in "application/config/".
suppose your controller name is 'Client_home'
Then,
eg : $route['default_controller'] = 'Client_home';
This is the reason why aboutus is working when you replace home in index() with aboutus . The index() method will be called by default, if you are not specifying which function you want to initiate.
For the aboutus() method / function, the route will be like this :
eg : $route['aboutus'] = "Client_home/aboutus";
Synatx:
$route['url'] = "controllername/method or function name"
Example for this question. Hope this help you:
http://localhost/ciexample/index.php/your_controller/your_function
i would like to know the controller name and also would like to know where u have change its configuration , if yes than which config .
use this in address bar
"localhost/folder_name_of_the_project/index/controller_name"

Codeigniter not finding file url helper

So I have this setup:
ARTISTAS
But when I click on that link, it takes me to http://localhost/Universal%20Music/Universal/artistas/index which says 404, like if there's nothing there but I've already created the controller with that name loading some views in it. Am I missing something here?
EDIT
My config.php has:
$config['base_url'] = 'http://localhost/Universal/Universal';
I stop using spaces in the names of the folders and now the path is:
C:\Users\Horacio\Documents\Projects\Universal\Universal
The controller file has the same name of the class I need and the code inside is:
<?php
class Artistas extends CI_Controller {
function __construct() {
parent::__construct();
}
public function index()
{
$this->load->view('header');
$this->load->view('main-index');
}
}

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