Accessing Variables from MY_Controller CodeIgniter - codeigniter

As I have read through articles, the best possible way to validate a user in all controllers of my application is to extend my own Controller that extends CI_Controller
But I can't make it work.
What I have is a members_controller that extends my MY_Controller.
in MY_Controller i have this piece of code, just to test if how am I going to utilize this.
<?php
class MY_Controller extends CI_Controller {
private $foo;
function __construct() {
parent::__construct();
$this->foo = 'hello world again';
}
}
in my members controller
<?php
class Members extends MY_Controller {
function index() {
$this->load->view('members');
}
}
in my members view
i want to access this variable i just set up, but i don't know how.
i tried echoing it out like this echo $foo, like this $this->foo
but neither work. thanks much!!

You need to pass the variable to your view file.
<?php
class Members extends MY_Controller {
function index() {
$data = array('foo' => $this->foo)
$this->load->view('members', $data);
}
}
Now you can refer to $foo inside your view. You may need to declare $this->foo as protected.
There's a video tutorial on MY_Controller that you may find useful at http://codeigniter.tv/a-10/Extending-the-core-MY_Controller-and-beyond

Have you tried passing the variable to the 'members view'.
$this->load->view('members',$foo);

You need to make $foo as protected and not private.
Then you will be able to access it in your controller that extends MY_Controller.php like this:
$this->foo

Today for the first time I used codeigniter 3. In the past I used version 2.x a lot of times. Now I can't extend CI_Controller:
application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
private $data = array();
function __construct() {
parent::__construct();
date_default_timezone_set("Europe/Zagreb");
$this->data['foo'] = "bar";
}
application/controllers/Welcome.php
class Welcome extends MY_Controller {
public function index() {
$this->data['main_content'] = 'home';
$this->load->view('template/template', $this->data);
}
application/view/home.php
(this is view loaded by $this->data['main_content'])
If I try to echo any data declared in MY_Controller, I get an error.
<?php echo $foo; ?>
A PHP ERROR WAS ENCOUNTERED
Severity: Notice
Message: Undefined variable: foo
Filename: template/header.php

Related

codeigniter : load second controller after execution of first controller

we have two controllers one is members.php which is registering the user another one is reserv.php for reserving a ticket for busses.
I want to load second controller after completion of first controller class.
members.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Members1 extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('member_model1');
}
public function index() {
$table = $this->member_model1->insert_members();
$data['members'] = $table;
//$this->load->view('header1', $data);
$this->load->view('reservation_view',$data);
//$this->load->view('members');
//$this->load->view('footer');
}
}
reserv.php
<?php
class reserve extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('member_model1');
}
function index() {
$table = $this->member_model1->get_members();
$data['members'] = $table;
//$this->load->view('header1', $data);
$this->load->view('reservation_view',$data);
//$this->load->view('reservation_view');
// $this->load->helper(array('form'));
//$this->load->view('reservation_view');
}
}
?>
To call a controller from another controller, you can use the function redirect() :
// In Members1 controller
redirect('/reserve/index');
Note that you won't be able to send data along with the redirect, such as when you load a view. To send data, you must use $this->session->set_flashdata(); because the redirects seem to use header() (I advise you to have a loot at this post).
// In Members1 controller
$this->session->set_flashdata('key', 'value');
redirect('/reserve/index');
By the way, when I look at your two classes, I wonder why you don't simply create a unique Ticket class with two methods like registerUser() and bookTicket() (these names are pure fancy, be free to use yours).

Autoload libraries in Codeigniter

I am a newbie to Codeigniter.
I have 3 libraries in autoload in config.php .
But in one of my controllers I don't want to load the libraries. Is this possible?
If you need any library throughout the application you can load it in the config file and it will be auto loaded. But if you need a library only in a specific controller you can load it in the controller where you need it.
Class test Extends CI_Controller{
function index()
{
$this->load->library('mylibrary');
$this->mylibrary->somemethod();
}
}
Or if you need library through out the controller you can load it in the constructor.
Class test Extends CI_Controller{
function __construct()
{
parent::__construct();
$this->load->library('mylibrary');
}
function index(){
$this->mylibrary->somemethod();
}
function test(){
$this->mylibrary->someothermethod();
}
}
Extend CI_Controller in your libraries.
Something like this:
class MyLibrary extends CI_Controller {
var $ci;
function __construct() {
$this->ci = &get_instance();
$route = $this->ci->router->fetch_class();
if( $route == strtolower('YourController') ) return false;
}
}
you can remove libraries from autoload file. then they will not be activ in the framwork.
If you want to use them, you can call them in constructors if you want them in a class. If you want to use them in just method, you load them in the method.

Loading codeigniter view with predefined variables

I do not know if the following is possible. If not other suggestions are appreciated.
In nearly all my controllers I will load some default views. For example header, footer and menu.
I would like to have certain variables auto load for each view.
If I take the header as an example. Into my header I will always load an array of $css scripts and an array of $javascript files.
$javascript[] = 'js/jquery.js';
$javascript[] = 'js/jqueryui.js';
But additionally, depending on the current page logic, I might want to add another javascript file to my $javascript variable.
$javascript[] = 'js/custom.js';
Then ideally, I would like these variables to be automatically inserted as data into the load of the view.
In other words, I just want to call:
$this->load->view('header');
How could this be achieved?
Create MY_Controller add a public array there then extend from MY_Controller
class MY_Controller extends CI_Controller {
public $data;
function __construct() {
parent::__construct();
$this->data['MYVAR'] = 'Something';
}
}
in your other controllers you just do it like this
class SomeClass extends MY_Controller {
function __construct () {
parent::__construct();
}
function index () {
$this->data['SomeOtherVar'] = 'xxx';
$this->load->view('viewname', $this->data);
}
}
You can use $this->load->vars in your Controller.
I use this in my_controller and all controllers are extend from MY_Controller
For example
<?php
class MY_Controller extends Controller{
public function __construct(){
parent::__construct();
$this->setGlobalViewVariables();
}
public function setGlobalViewVariables(){
$result = array();
$result['value1'] = 'value1';
$result['value2'] = 'value1';
$this->load->vars($result);
}
}
?>
you should create an hook for this, it is very simple

Model Fails To Load

I am having issues with my Code Ignitor, in that I load my model the code fails, without leaving any trace.
I am attempting to load my model like so
<!--CONTROLLER-->
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Concierge extends Public_Controller {
public function submit()
{
$this->load->model('Concierge_model');
}
My model is set up as below:
<!--MODEL-->
<?php
class Concierge_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
function saveRequest($action, $owner)
{
$query = $this->db->query('select * from table');
return $query;
}
Does anyone see what might be the issue that is causing my problem? I am using CodeIgnitor 2.1.2.
Assuming Public_Controller is something custom, make sure that class extends CI_Controller and also make sure the parent's contruct function is called there.
class Public_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
}
}
I don't know what the problem was, I deleted the model, view, and controller and recreated them. This fixed the issue, but sadly didn't shed any light onto what caused it.

How to Inherit A Model from Another Model in CodeIgniter

i'm using codeigniter for my project and i have this class model which i call Genesis which looks like this:
class Genesis_model extends CI_Model {
function __construct() {
parent::__construct();
}
function get() {
return 'human soul';
}
}
and i have another model, stored in the same directory, which extends Genesis_model
class Human_model extends Genesis_model {
function __construct() {
parent::__construct();
}
function get_human() {
return $this->get();
}
}
Human_model is used by Human controller
class Human extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('human_model');
}
function get_human() {
$data['human'] = $this->human_model->get_human();
$this->load->view('human/human_interface', $data);
}
}
if i execute the code, it will produce an error which point to return $this->get(). it reads "Fatal error: Class 'Genesis_model' not found in ...\application\models\human_model.php on line 2".
i use this method because nearly all my models shared almost identical structure. I gather the similar functionality in Genesis while the other models serve only as data suppliers unique to the tables they represent. it works well in my asp.net (vb.net) but i don't how to do it in codeigniter.
is there a way for Human_model to inherit Genesis_model. i don't think i'm allowed to use include('genesis_model.php'). i don't know if it works either.
thanks in advance.
core/MY_Model is good if there's only 1 important superclass for your models.
If you want to inherit from more than model superclass, a better option is to change your autoload configuration.
In application/config/autoload.php, add this line:
$autoload['model'] = array('genesis_model');
Put the file genesis_model.php in the core directory
Change your Human_model to this:
include('genesis_model.php');
class Human_model extends Genesis_model {
function __construct() {
parent::__construct();
}
function get_human() {
return parent::get();
}
}
notice the get_human function and the include.
You have to include the Genesis_model on your Human_model.php like this:
include_once( APPPATH . 'folder/file' . EXT );
Or you can autoload it on your config/autoload.php file, what I think is stupid =)
other solution
<?php
$obj = &get_instance();
$obj->load->model('parentModel');
class childModel extends parentModel{
public function __construct(){
parent::__construct();
}
public function get(){
return 'child';
}
}
?>

Resources