I have the following simplistic code:
// FILE: controllers/Top.php
class Top extends MY_Public_Controller {
function __construct() {
}
public function Top() {
echo 'Hello';
}
}
// FILE: application/core/MY_Public_Controller.php
class MY_Public_Controller extends MY_Controller {
function __construct() {
parent::__construct();
}
}
// FILE: application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
function __construct() {
parent::__construct();
}
}
And I get the following the following error:
Fatal error: Class 'MY_Public_Controller' not found in
/var/www/example.com/public_html/application/controllers/Top.php on line 5
A PHP Error was encountered
Severity: Error
Message: Class 'MY_Public_Controller' not found
Filename: controllers/Top.php
Line Number: 5
Backtrace:
Any help would be much appreciated!
Instead of you create a new file (MY_Public_Controller.php) to create the class My_Public_Controller.
Insert this class inside the My_Controller.php file.
In that way the My_Controller.php file will be like:
class MY_Controller extends CI_Controller {
function __construct() {
parent::__construct();
}
}
class MY_Public_Controller extends MY_Controller {
function __construct() {
parent::__construct();
}
}
After I see another answer
Or you can make something like #Hikmat Sijapati said, but instead of you put the require_once, inside the My_Controller.php. Try to put it in the My_Public_Controller.php using 'My_Controller.php' as parameter. Something like that:
My_Public_Controller.php:
include_once('My_Controller.php');
class MY_Public_Controller extends MY_Controller {
function __construct() {
parent::__construct();
}
}
I have not tried it that way, but I think it will work.
Try like this...
You can create any number of controller but create controller's must be included in the controller that extends CI_Controller.As Below:
Controller's Name and function Name Keep different (Good Way)
MY_Controller:application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
function __construct() {
parent::__construct();
}
include_once('MY_Public_Controller.php');// include here
}
MY_Public_Controller: application/core/MY_Public_Controller.php
class MY_Public_Controller extends MY_Controller {
function __construct() {
parent::__construct();
}
}
And Top: application/Top.php
class Top extends MY_Public_Controller {
function __construct() {
}
public function index() { //function name must be different than controller's name
echo 'Hello';
}
}
Related
I have a MY_Controller php file with MY_Controller class and Other_Controller that extends My_Controller class in my application/core folder.
class MY_Controller extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function SomeMethod()
{
echo "method MY_Controller";
}
}
class Other_Controller extends My_Controller
{
function __construct()
{
parent::__construct();
}
}
On my Application/controller folder :
Class Main extends Other_Controller
{ include(APPPATH.'core/Other_Controller.php');
function __construct()
{
parent::__construct();
// Call SomeMethod function name?
}
}
Can I call SomeMethod function from MY_Controller to Main Controller?
Yes, you can, simply use parent keyword same way as you're using it with constructor:
class Main extends Other_Controller
{
function __construct()
{
parent::__construct();
parent::SomeMethod(); // echoes "method MY_Controller"
}
}
In case Other_Controller class overrides MY_Controller::SomeMethod, you still can call the original SomeMethod from the Main class by using full class name and scope resolution operator :::
class MY_Controller extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function SomeMethod()
{
echo "method MY_Controller";
}
}
class Other_Controller extends My_Controller
{
function __construct()
{
parent::__construct();
}
function SomeMethod()
{
echo "method Other_Controller";
}
}
class Main extends Other_Controller
{
function __construct()
{
parent::__construct();
parent::SomeMethod(); // echoes "method Other_Controller"
MY_Controller::SomeMethod(); // echoes "method MY_Controller"
}
}
What I'm trying to do on the surface seems simple, basic OOP PHP but I just can't get it working. I have a controller class which is calling a model, that model extends another model of mine but it throws an error saying it can't find it:
Controller (Welcome.php):
public function __construct()
{
parent::__construct();
$this->load->model('users_model');
}
public function index()
{
$this->users_model->getAll();
}
Users Model (User_model.php):
class Users_model extends Base_model
{
public function __construct()
{
parent::__construct();
}
}
Base Model (Base_model.php):
class Base_model extends CI_Model
{
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function getAll($table)
{
$query = $this->db->query('Query here');
return $query;
}
}
This gives me the error Fatal error: Class 'base_model' not found in /ci/application/models/Users_model.php on line 3
Save your Base_model in application/core named as Base_model.php with following code.
class Base_model extends CI_Model
{
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function getAll($table=FALSE)
{
$query = $this->db->query('Query here');
return $query;
}
}
Save User_model in application/models named as User_model.php having following code
class User_model extends Base_model
{
public function __construct()
{
parent::__construct();
}
}
Then make a controller Welcome.php in appliation/controllers having following code with extending CI_Controller
public function __construct()
{
parent::__construct();
$this->load->model('user_model');//loads user_model
}
public function index()
{
$data = $this->user_model->getAll(); //need a variable to hold return data
}
You just have to locate the Base_model in your Users_model like below code and you can access the functions of base model easily.
require('application/models/base_model.php');
class User_model extends Base_model
{
function __construct()
{
parent::__construct();
}
}
I got error when I run this code
class MY_Controller extends CI_Controller {
public function MY_Controller () {
var_dump ( $this->config->item ('default_app') );
}
}
Error:
Message: Undefined property: Welcome::$config
My MY_Controller is in Core folder & I use Codeigniter 3.0.6. The error tells that variable $config is not found.
Change
class MY_Controller extends CI_Controller
{
public function MY_Controller()
{
var_dump ( $this->config->item('default_app') );
}
}
with
class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
var_dump ( $this->config->item('default_app') );
}
}
I will extend the controller based of login typical, so I create on application directory like this:
core
-MY_Controller
-public controller
MY_Controller
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
}
public controller
<?php
class Public_Controller extends MY_Controller
{
//Some Logic here
public $layout = 'layout';
}
Now, time to use those things.
I write on application/route $route['default_controller'] = 'Home';
So, the controll that named Home would be like this :
<?php
class Home extends Public_Controller {
public function index() {
$this->load->view('public/home');
}
}
But unfortunately, it gives me error like this :
Fatal error: Class 'Public_Controller' not found in C:\wamp\www\egi\application\controllers\Home.php on line 5
Why I can not to extends the sub class ?
NOTE
But if I extends from MY_Controller, its success
<?php
class Home extends MY_Controller {
public function index() {
$this->load->view('public/home');
}
}
Any help it so appreciated
You can do this in one of the following ways.
Have a file named Public_controller in "application/core" & include that file in your application/core/MY_Controller.php file.
Define Public_Controller inside MY_Controller file which is autoloaded by default which means you'd end up with something like this.
File: application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
public function __construct () {
parent::__construct();
}
}
class Public_Controller extends CI_Controller {
public function __construct () {
parent::__construct();
}
}
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();
}
}