Variable config not found in MY_Controller Codeigniter 3 - php

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') );
}
}

Related

Codeigniter, problems with extending classes

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';
}
}

Access subclass in codeigniter 3.0.1

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();
}
}

Codeigniter $data returns emty

Hello guys I have a problem with calling array which is declared in MY_Controller and want to use in Welcome controller. I'm new with ci so I think it's a simple problem or I has forgot something to load in config... (I'm using ci 2.2)
MY_Controller :
class MY_Controller extends CI_Controller {
public $data = array();
function __construct() {
parent::__construct();
$data['test'] = 'Hello World';
}
}
Welcome controller >
class Welcome extends MY_Controller {
public function index()
{
var_dump($this->data);
$this->load->view('welcome_message');
}
}
and the result is :
array (size=0)
empty
Why is my array empty, why not "Hello world"?
Change your MY_Controller constructor to below:
class MY_Controller extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->data = array('test' => 'Hello World');
}
}
No need of this:
public $data = array();
Explanation:
To access the members of the same or parent class you have to use the keyword $this.
Example:
1) To access/declare a variable:
$this->variable_name;
2) To access a method/function:
$this->function_name();

How to use $layout in Laravel controller

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');
}
}

Get error on first codeigniter app

I am new to codeigniter and I was trying my first app from the reference of this link
But I got the following error:
Fatal error: Class 'Controller' not found in /var/www/CodeIgniter/application/controllers/helloworld.php on line 2
Thi should be done like this
Class Mycontroller Extends CI_Controller
{
function __construct(){
parent::__construct();
}
//other functions
}
Class Mymodel Extends CI_Model
{
function __construct(){
parent::__construct();
}
//other functions
}

Categories