CodeIgniter - Replace redunant JSON conversion - ajax

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.

Related

Laravel 4: Reference controller object inside filter

I have a controller in Laravel 4, with a custom variable declared within it.
class SampleController extends BaseController{
public $customVariable;
}
Two questions: Is there any way I can call within a route filter:
The controller object where the filter is running at.
The custom variable from that specific controller ($customVariable).
Thanks in advance!
as per this post:
http://forums.laravel.io/viewtopic.php?pid=47380#p47380
You can only pass parameters to filters as strings.
//routes.php
Route::get('/', ['before' => 'auth.level:1', function()
{
return View::make('hello');
}]);
and
//filters.php
Route::filter('auth.level', function($level)
{
//$level is 1
});
In controllers, it would look more like this
public function __construct(){
$this->filter('before', 'someFilter:param1,param2');
}
EDIT:
Should this not suffice to your needs, you can allways define the filter inside the controller's constructor. If you need access to the current controller ($this) and it's custom fields and you have many different classes you want to have that in, you can put the filter in BaseController's constructor and extend it in all classes you need.
class SomeFancyController extends BaseController {
protected $customVariable
/**
* Instantiate a new SomeFancyController instance.
*/
public function __construct()
{
$ctrl = $this;
$this->beforeFilter(function() use ($ctrl)
{
//
// do something with $ctrl
// do something with $ctrl->customVariable;
});
}
}
EDIT 2 :
As per your new question I realised the above example had a small error - as I forgot the closure has local scope. So it's correct now I guess.
If you declare it as static in your controller, you can call it statically from outside the controller
Controller:
class SampleController extends BaseController
{
public static $customVariable = 'test';
}
Outside your controller
echo SampleController::$customVariable
use:
public function __construct()
{
$this->beforeFilter('auth', ['controller' => $this]);
}

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

Convenient Way to Load Multiple Databases in Code Igniter

I've added the appropriate configuration arrays to database.php and they work, however, I would like an easier way to access the different databases. Right now I have to do something like this in every controller method:
function index(){
$BILLING = $this->load->database('billing', TRUE);
$INVENTORY = $this->load->database('inventory', TRUE);
$data['billing'] = $BILLING->get('stuff');
$data['inventory'] = $INVENTORY->get('stuff');
}
I'd like to be able to put those first two lines in some sort of before filter or pre_controller hook.
You could simply load the database instances globally in your constructor, then they would be available to all controller methods...
example controller
class Example extends CI_Controller {
//declare them globally in your controller
private $billing_db;
private $inventory_db;
function __construct() {
parent::__construct();
//Load them in the constructor
$this->billing_db = $this->load->database('billing', TRUE);
$this->inventory_db = $this->load->database('inventory', TRUE);
}
function index() {
//Then use them in any controller like this
$data['billing'] = $this->inventory_db->get('stuff');
$data['inventory'] = $this->billing_db->get('stuff');
}
}
And if these same databases are used across multiple controllers, you might consider extending the base controller to include these global variables and load them in the constructor of your base controller in MY_Controller.php
example MY_Controller.php
class DB_Controller extends CI_Controller {
//declare them globally in your controller
private $billing_db;
private $inventory_db;
function __construct() {
parent::__construct();
//Load them in the constructor
$this->billing_db = $this->load->database('billing', TRUE);
$this->inventory_db = $this->load->database('inventory', TRUE);
}
}
Then you'd use it like this...
class Example extends DB_Controller {
function __construct() {
parent::__construct();
}
function index() {
//Then use them in any controller like this
$data['billing'] = $this->inventory_db->get('stuff');
$data['inventory'] = $this->billing_db->get('stuff');
}
}

Resources