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"
}
}
Related
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 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';
}
}
Suppose I have two child controllers of MY_Controller,say Child_1 & Child_2.In MY_Controller I have two methods method_1 & method_2 as
abstract class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public abstract function method_1();//children will give custom implementation
public function method_2()
{
some code ...
/*Here I want to call method_1() of the child controller that have called this method i.e. method_2* automatically */
}
}
I am calling method_2() from child_1 & child_2 using
class Child_1 extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
public function method_1(){//custom implementation goes here}
public function some_method()
{
...some code
$this->method_2();//call inherited method method_2()
}
}
A similar code for child_2
abstract class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public abstract function method_1();//children will give custom implementation
public function method_2()
{
// some code ...
$this->method_1(); //Call the method directly it will implement the child's method
/*Here I want to call method_1() of the child controller that have called this method i.e. method_2* automatically */
}
}
I try to use the $layout property in my controllers, but I always get the following error:
Call to a member function nest() on a non-object
When dd() the value of this->layout, it seems like it is only a string.
Here's my code:
base.php
class Base_Controller extends Controller {
public $layout = 'layouts.common';
}
admin.php
class Admin_Controller extends Base_Controller {
public function action_index() {
$this->layout->nest('content', 'admin.home');
}
}
I probably miss something, but I can't find it in the docs
Ahhh, just found the trouble. I was ovewritting __construct to add filters, but wasn't calling the parent construct:
class Admin_Controller extends Base_Controller {
public function __construct() {
parent::__construct();
$this->filter('before', 'auth');
}
public function action_index() {
$this->layout->nest('content', 'admin.home');
}
}
I'm trying out codeigniter for the first time on a MAMP deployment however I'm unable to load a model into a controller. The end result is always...
Unable to locate the model you have specified: second_model
Controller - welcome.php
class Welcome extends CI_Controller {
public function index()
{
$this->load->model('second_model');
$this->load->view('welcome_message');
}
}
Model - second_model.php
class Second_model extends Model {
public function __construct()
{
// model constructor
parent::__construct();
}
}
You should be extending CI_Model rather than Model
So:
class Second_model extends CI_Model {
public function __construct()
{
// model constructor
parent::__construct();
}
}