Autoload libraries in Codeigniter - 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.

Related

CI_Template' not found

I want to load model using
$this->load->model('apotek_data');
function __construct()
{
parent::__construct();
$this->load->model('apotek_data');
$this->template->write_view('sidenavs', 'template/default_sidenavs', true);
$this->template->write_view('navs', 'template/default_topnavs.php', true);
$this->load->database();
}
But when I put it ($this->load->model) in my code, the view doesn't show, and display an error message.
Fatal error: Class 'CI_Template' not found in
D:\wamp64\www\apotek\system\core\Common.php on line 196
What should I do?
Load the template library:
$this->load->library('template');
OR:
In config/autoload.php add template in the libraries array.
I don't know the name of your library so I am assuming it is template if it is not - change it with the name of the library.
For any other person experiencing a 'CI_Template not found' error:
In my case, I was loading a library (in the constructor) with the same name as the controller that was being accessed.
class Authld extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->library('Authld');
}
}
I had to change the name of the controller for the issue to be resolved.
class Authentication extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->library('Authld');
}
}
Your Template.php maybe looks like this:
class Template extends MX_Controller{
If so, change the MX to CI (CI_Controller).

CodeIgniter - Replace redunant JSON conversion

I recently started using Codeigniter after having a structural problem in one of my Ajax-heavy applications. (You can read up on it if you want in my previous question)
I have a fairly short question. Currently I am making a lot of Ajax requests to different controllers. I open the controllers like this:
public function __construct()
{
parent::__construct();
$this->output->set_content_type('application/json');
}
And at the end of every function I do the following:
$this->returnValue['result'] = "ReturnedInfo";
$this->returnValue = json_encode($this->returnValue);
$this->output->set_output($this->returnValue);
The code is pretty clear in itself, but I don't want to keep repeating myself. The codeigniter manual says to do the following:
$this->output
->set_content_type('application/json')
->set_output(json_encode(array('foo' => 'bar')));
But I would still be repeating myself. Also, I don't want to add a function to every controller that does this, even if it does decrease redundancy.
Since all of my controllers return JSON, is there a way to set this globally in a config file maybe, or in any other way?
TL;DR I have this same piece of code in every controller/function. Since the output type is always the same, just not the result, is there a way to automate this process across every controller/function?
Create an Ajax_Controller that extends MY_Controller that extends CI_Controller.
The Ajax Controller will then inherit from both Controllers.
class Ajax_Controller extends MY_Controller
{
public function __construct()
{
parent::__construct();
if(!$this->input->is_ajax_request()) return show_error('Invalid Request');
}
public function jsonOutput($json)
{
//some data checking here....
return $this->output
->set_content_type('application/json')
->set_header("HTTP/1.1 200 OK")
->set_output($json);
}
}
-
class User extends Ajax_Controller
{
public function __construct()
{
parent::__construct();
}
public function userMethod()
{
$json = json_encode(array(
'' => ''
));
$this->jsonOutput($json);
}
}
Extend your controllers from your own base class rather than CI_Controller and put your repeatedly-used function(s) and constructor code in there. Something like:
class BaseController extends CI_Controller {
protected function index() {
$this->returnValue['result'] = "ReturnedInfo";
$this->returnValue = json_encode($this->returnValue);
$this->output->set_output($this->returnValue);
}
}
class Specific extends BaseController {
public function index() {
//do controller-specific stuff
parent::index();
}
}
I abstract this further if I have groups of controllers with shared code; for example, if I had a bunch of controllers that require the user to be logged-in I create AuthenticatedController, which extends BaseController and add session checks etc.

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

base controller and apply it to all existing controller

I need to create codeigniter base controller to check allowed ip address in database by mobel function if the ip is exists then user should go to home page but if the ip address is not exists and show 404 page in codeigniter, i can't find core folder in application folder
First, you need to extend a core class, call it MY_Controller.php
Save that file in: application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('ip_table_model');
$this->load->library('input');
// assuming there's a function called "check_ip($ip_address)" in ip_table_model
if (!$this->ip_table_model->check_ip($this->input->ip_address()) {
redirect('error_404');
}
}
}
Now, we're assuming you have a model called ip_table_model which connects to database with list of IP addresses, and there's a function called check_ip which will validate whether user has access or not. This is relatively simple, and I won't show any examples on this.
The redirect('error_404'); page does not yet exist, you need to create a controller which shows your 404 page.
Now, for any other controllers in your project, instead of extends CI_Controller, make them extend MY_Controller instead.
Here's an example:
class Welcome extends MY_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->view('welcome_message');
}
}
Explanation: We're extending CI_Controller to create our own core controller, called MY_Controller. Inside, we're checking if user has access or not through the constructor, which will be called in every other controller in the project.
References:
http://codeigniter.com/user_guide/general/core_classes.html
http://codeigniter.com/user_guide/libraries/input.html
Answer is here (section Extending Core Class).
1.7.2 has a different structure to 2.0.*, therefore there is no core folder in application
In Core Create a new Class .
Name MY_Controller.php
class MY_Controller extends CI_Controller {
// Write your functions here which you wanna use throughout the website
public function abc (){
echo "Helllo";
}
}
class Welcome extends MY_Controller {
function __construct()
{
parent::__construct();
}
function your_custom_fuctions()
{
$this->abc(); //echo Hello...
//Anything you want to do
}
}
function admin_view($view_name = "", $header_info = NULL, $sidebar_info=NULL,$page_info = NULL, $footer_info = NULL, $data_info = ""){
$this->load->view('Admin/includes/header', $header_info);
$this->load->view('Admin/includes/Left_sidebar', $sidebar_info);
$this->load->view($view_name, $page_info);
$this->load->view('common/footer', $footer_info);
}

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