I'm doing an app and I'm using a base controller to create others ones. When I launch a controller I see that error:
Fatal error: Class 'BaseController' not found in C:\xampp\htdocs\sFitness\api\1-dev\controllers\PeopleController.php on line 3
The problem is that in the same folder I've BaseController.php
following I send you the PeopleController.php:
<?php
class PeopleController extends BaseController {
public function processGetRequest($request) {
$result = NULL;
$result = $this->_model->getAllAthletes();
return $result;
}
}
?>
following the BaseController.php
<?php
abstract class BaseController {
public $_model;
public function __construct() {
$this->_model = new Model();
}
}
?>
How can I resolve this problem? What's the problem?
Thanks so much
You should import your other file so php can see it;
in PeopleController, do the following:
<?php
require_once "BaseController.php";
class PeopleController extends BaseController
{
public function processGetRequest($request)
{
$result = NULL;
$result = $this->_model->getAllAthletes();
return $result;
}
}
?>
Related
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.
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.
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
I have a controller:
class Blah extends Controller
{
function Blah()
{
$this->load->model('baga_model');
}
}
then comes baga_model:
class Baga_model extends Model
{
function do_it()
{
echo "BOOM!";
}
}
..and
class Blah_model extends Model
{
function some_action()
{
$this->baga_model->do_it();
}
}
So .. when in blah_model I call $this->baga_model->do_it() I get an error :
Call to a member function do_it() on a non-object
I just can't understand why.... I know it must work, I did something similar before..
Thanks
Got it! I had to load baga_model in blah_model constructor. This way it works.
Thanks everyone.
public function test()
{
$this->load->model('baga_model');
$this->baga_model->do_it();
}
Model
class baga_model extends CI_Model
{
public function do_it()
{
echo $this->bar("BOOM!");
}
Your not loading your required model inside your model:
class Blah_model extends CI_Model
{
$this->baga_model = $this->load->model('baga_model', true);
public function some_action()
{
$this->baga_model->do_it();
}
}
I am trying to port an application's few functions from a CodeIgniter application to another existing CodeIgniter application. Both applications on themselves are working very well but when I added this thing it gives the following error:
Fatal error: Call to a member function order_by() on null in …\application\core\MY_Model.php on line 7
In this question I have removed parts unrelated to the error to simplify code.
//MY_Model.php model file
<?php
class MY_Model extends CI_Model {
protected $_order_by = '';
public function get(){
$this->db->order_by($this->_order_by);
}
}
//article_m.php model file
<?php
class Article_m extends MY_Model
{
protected $_order_by = 'pubdate desc, id desc';
}
//frontend.php controller file
<?php
class Frontend extends MY_Controller
{
function __construct()
{
$this->load->model('article_m');
}
function index()
{
$this->article_m->get();
}
}
Please help. Thank you!
whenever calling any $this->db ... you have to make sure to load your database library. Check in application\config\autoload.php for the following:
$autoload['libraries'] = array('database');