Codeigniter overriding show_404 cant load view - php

file:MY_Exceptions.php,
class MY_Exceptions extends CI_Exceptions {
public function __construct(){
parent::__construct();
}
public function show_404($page = ''){
//from helper, helper's loading..correctly. so are libs
$data=common_site_data();
$CI->load->view('site/templates/header',$data);
$CI->load->view('site/home/404',$data);
$CI->load->view('site/templates/footer',$data);
}
}
But no views are loading..! Not even the errors/error_404.php..!
What's the heck? Can anyone help this out?

Related

How to load view file in core functions in codeigniter

Am trying to load view file in application/core/MY_Controller.php but its giving as follows.
Message: Undefined property: Edustaticlanding::$load
Filename: core/MY_Controller.php
Function is as follows in MY_Controller.php
function menu(){
$arrr = array();
return $arrdata['menu'] = $this->load->view('menu',$arrr,true);
}
And am calling this function in controller(Edustaticlanding.php) as follows.
function __construct(){
$this->menucontent = $this->menu();
print_r($this->menucontent); die;
}
Pls correct me.. where its going wrong.. thanks
On application/core/MY_Controller.php
Add public $data = array() like below
<?php
class MY_Controller extends CI_Controller
{
public $data = array();
public function __construct()
{
parent::__construct();
$this->data['menu'] = $this->menu();
}
public function menu(){
return $this->load->view('menu', NULL, TRUE);
}
}
On the controller then once add public $data = array(); you can access the menu on view
You have to use $this->data now on controller
<?php
class Example extends MY_Controller {
public $data = array();
public function index() {
$this->data['title'] = 'Welcome to Codeigniter';
$this->load->view('example', $this->data);
}
}
On the example view now you can echo
<?php echo $menu;?>
Add extends CI_Controller to your core controller like following codes:
class MY_Controller extends CI_Controller {
Please check below mentioned solution. You need to call parent constructor first. So it will load all basic configurations.
First extent CI_Controller class and call parent constructor like describe below
class MY_Controller extends CI_Controller {
public function __construct{
parent::__construct();
}
}
Please let me if it not works.

Unable to locate the specified class: Session.php, When call a function from another controller in Codeigniter

contrller:News.php
This is my controller News
<?php class News extends CI_Controller {
public function __construct()
{
}
public function getShowIN_News()
{
return $result;
} } ?>
contrller:Category.php
This is my controller Category
<?php class Category extends CI_Controller {
public function __construct()
{
}
public function category()
{
require('news.php');
$test = new News();
$data["headlines"] = $test->getShowIN_News();
} }?>
By using an empty constructor, you're making it so that CI_Controller::__construct() isn't called, and that's where everything in the framework is initialized.
I know you've put it there to hack it so you can call one controller from another, but it is very intentionally made that way, exactly so you don't do this.

How to load a model in a master controller on codeigniter framework?

I am creating a master controller so that every other controllers on my app extend the master (MY_Controller).
My problem is how to make loading a model more abstract, let me show you the code to better explain.
class MY_Controller extends CI_Controller
{
protected $model;
function __construct()
{
parent::__construct();
}
function get($order_by)
{
$this->load->model($this->model);
$query = $this->$this->model->get($order_by);
return $query;
}
}
I declare a protected variable $model in the master controller so that on the extending controller i can asign it a value:
class Home extends MY_Controller {
public function __construct() {
parent::__construct();
$this->model = "home_model";
}
public function index()
{
$test = $this->get('id');
}
}
The problem is that on MY_Controller i cant load the model
$this->$this->model->get();
i get the following error: Message: Object of class Home could not be converted to string
any help will be appreciated , thank you !
Why do not save the value you assign to protected global $model variable in a variable inside the function as such:
class MY_Controller extends CI_Controller{
protected $model;
function __construct(){
parent::__construct();
}
function get($order_by){
$_model = $this->model // I added this line.
$this->load->model($_model); // I modified this line.
$query = $this->$_model->get($order_by); // I modified this line.
return $query;
}
}
The $this->$this->model->get() is your problem. If you want to use an object property ($this->model) inside a chain like this, you need to wrap it in braces: $this->{$this->model}->get().

Codeigniter Error Unable to locate the specified class: Loader.php

i am trying to make a core class in my codeigniter but its giving error that Unable to locate the specified class: Loader.php.
My Core class is MY_base.php and code is.
class MY_base extends CI_Controller{
public function load_header(){
$this->load->model('mod_practice');
$headData=$this->model->get_header();
$this->load->view('header',$headData);
}
}
My model Mod_practice.php code is
class Mod_practice extends CI_Model{
public function get_header(){
$query = $this->db->get('header');
$result = $query->result_array();
return $result;
}
}
My home.php ( main controller) code is
class Home extends MY_loader{
function index(){
parent::MY_base();
}
}
but when i try to run Home controller its giving me the following error
Unable to locate the specified class: Loader.php.
Where can be the error ? Thanks in advance.
You are doing something wrong. You need to construct CI_controller first.
class MY_base extends CI_Controller{
public function __construct()
{
parent::__construct();
// Your own constructor code
}
public function load_header(){
$this->load->model('mod_practice');
$headData=$this->model->get_header();
$this->load->view('header',$headData);
}
}
And now you can do this:
class Home extends MY_base{
public function __construct()
{
parent::__construct();
// Here you have access to load_header() function
}
}
Ou - and also your create MY_base and then refer to MY_loader.
may bee you can use HMVC Codeigniter https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc

Why does my code end with: Call to a member function sample_template() on null

I am using hierarchical MVC model in codeigniter. I create a controller called template and inside it a function called sample_template. Then a view called sample_template_v created and call it inside the template controller. I create another controller called Admin and called Template->sample_template($data); inside its 2 funtions.
MY_Controlle.php
<?php
class MY_Controller extends MX_Controller
{
function __construct()
{
parent::__construct();
$this->load->module('Template');
}
}
Admin.php
<?php
class Admin extends MY_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
$data['content_view'] = 'admin/admin_v';
$this->Template->sample_template($data);
}
function about()
{
$data['content_view'] = 'admin/about_v';
$this->Template->sample_template($data);
}
}
Template.php
<?php
class Template extends MY_Controller
{
function __construct()
{
parent::__construct();
}
function sample_template($data = NULL)
{
$this->load->view('Template/sample_template_v', $data);
}
}
sample_template_v.php file---->
<h5>This is the main Template.</h5>
<?php $this->load->view($content_view); ?>
Error:
If you want to call a method from the object you need to initiaze the object and then call a method. Make sure $this->template is set in your case it isn't.
$this->template = new Template();
$this->template->sample_template($data);

Categories