Codeigniter - making my controllers more DRY - php

In my codeigniter controller function I am using the following code to generate my view and insert all the necessary content:
$left_column = $this->load->view('widgets/sub_navigation', $subnav_data, true);
$left_column .= $this->load->view('widgets/search_box', '', true); //Set data to be loaded into columns
$left_column_base = $this->load->view('widgets/assist_navigation', '', true);
$center_column = 'this is center column';
$right_column = $this->load->view('widgets/ask_us_a_question', '', true);
$right_column .= $this->load->view('widgets/newsletter', '', true);
$right_column .= $this->load->view('widgets/latest_news', '', true);
$this->template->inject_partial('left_column', $left_column); //Inject data into the partial columns
$this->template->inject_partial('left_column_base', $left_column_base);
$this->template->inject_partial('center_column', $center_column);
$this->template->inject_partial('right_column', $right_column);
$this->template->build('template',$data);
I am using a three column layout and the code above dictates what is shown in each of the columns. It works in a very modular way allowing me to customize each page quickly.
Is there a way of simplifying the above code, using arrays maybe, to cut down on repetetive code, making things more DRY??

You need to create base controllers that extend the CI_Controller. Then all your controllers extend a certain base controller you created, depending on what needs to be done in all cases that controller is called.
In application/core create a file called MY_controller.php (the prefix can be changed in config):
class MY_Controller extends CI_Controller {
function __construct()
{
parent::__construct();
/* Widgets are only prepared -- they will be fetched and rendered once layout->render is called.
This saves the overhead of reading the files on requests where layout isn't rendered.
*/
$this->layout->prepare_widget( "widgets/navigation", "navigation_widget" );
$this->layout->prepare_widget( "widgets/footer", "footer_widget" );
$this->layout->prepare_widget( "widgets/twitter", "twitter_widget" );
}
}
class Public_Controller extends MY_Controller {
function __construct()
{
parent::__construct();
}
}
class Admin_Controller extends MY_Controller {
function __construct()
{
parent::__construct();
if( !$this->user->has_permissions( PERMISSION_ADMIN ) )
{
redirect( base_url(), "location", 303 );
die();
}
}
}
class Member_Controller extends MY_Controller {
function __construct()
{
parent::__construct();
if( !$this->user->has_permissions( PERMISSION_REGISTERED ) )
{
redirect( base_url(), "location", 303 );
die();
}
}
}
As you can see, all sub controllers have the widgets automatically because they extend either public, admin or member. A sub controller extending an admin controller has automatically the permissions checked so you don't need to do that ever again. You can apply this concept to your app.
A sub-controller: (placed in the normal application/controllers)
class Articles extends Member_controller {
...
}
Will have automatically ensured that the user is logged in, and the widgets are prepared without doing anything because the parent of parent class already prepared them. All you need to do in articles is to call $this->layout->render if the logic needs layout rendering at the end.

Codeigniter controllers are designed after the transaction script pattern, it's known that controller tend to get large and "not DRY" when you application grows.
To prevent such, you can re-implement the view to handle a two-step compound view pattern supporting layouts. Look for a layout view IIRC there are some on the codeigniter site.

I wrote a blog post explaining my design philosophy on organizing CodeIgniter controllers to make them more DRY. I like to have the index function of my controller serve as a common entry/exit point to avoid repeating operations that are common to all controller methods.
http://caseyflynn.com/2011/10/26/codeigniter-php-framework-how-to-organize-controllers-to-achieve-dry-principles/

Related

Make things available in any view file Laravel

I have a question about making some elements available in any view file
Lets say im building a webshop and on every page i will be having my sidebar, shoppingcart, user greeting in the top.
How can i make these things available in all my view files?
I could make a class, lets say class Frontend
I could do something like this:
class Frontend {
static $me;
public function get(){
if(!self::$me){
self::$me = new self();
}
return self::$me;
}
private function getShoppingCart(){
// do things
}
public function getData(){
return array(
'Username' => User::find(1)->UserName,
'Cart' => $this->getShoppingCart()
);
}
}
Now in my controller i could pass this Frontend object into the view
View::make('file.view')->with(array('data' => Frontend::get()->getData()));
But this way i will end up with a god class containing way too much stuff and in every controller method i would have to pass these data, which is not relevant to the controller method
Is there a way in Laravel that makes specific data available across all view files?
Thanks!
Use share:
View::share('name', 'Steve');
as per http://laravel.com/docs/responses#views
To keep everything clean, every part of the page should be its own *.blade.php file which would be put together using a master template of sorts.
master.blade.php
#yield('includes.sidebar')
#yield('users.greeting')
#yield('store.shoppingcart')
Then you can use view composers so that each time these views are loaded, the data you want is injected into them. I would probably either create a new file which would get autoloaded, or if you have service providers for the separate portions of your app that these views would use, it would also go great in there.
View::composer('users.greeting', function($view)
{
$view->with('user', Auth::user());
});
In this case, it would make the user model available inside your view. This makes it very easy to manage which data gets injected into your views.
You are close with your 'god class' idea.
Setting a $data variable in a basecontroller has helped me with similar issues
class BaseController extends Controller {
protected $data;
public function __construct() {
$this->data['Username'] = User::find(1)->UserName
$this->data['Cart'] = $this->getShoppingCart()
}
}
class Frontend extends BaseController {
function someMethod(){
View::make('file.view', $this->data)
}
}

Load a single module of page in views codeigniter framework

I am developing a web app using codeigniter framework. My application has a fixed header and footer.I want to middle section (ie the body) of my application to load when the user goes to various pages which are available to him and make the header and footer constant.
(hackerrank.com ... I was talking about a website similar to this one ... after login into this website ... the header and the sidebar of this remains constant and they load the remaining page ... how can we implement it using CI framework)
Is there any way through which I can achieve this?
I am performing the following actions which is making me to load the complete website(Its like reloading the entire page :/)
As You can see the following code is present in my template.php
<?php
$this->load->view("templates/header.php");
$this->load->view($main_body);
$this->load->view("templates/footer.php");
?>
and I Controller I write the following piece of code usually
public function load_page(){
$data['main_body'] = 'dashboard_pages/dashboard_view';
$this->load->view('template.php',$data);
}
You were pretty close to the solution: Load the header and the footer as they are, variables:
<?php
$this->load->view($header);
$this->load->view($main_body);
$this->load->view($footer);
?>
The tricky thing, is that you're always writing the method function load_page() in every controller you write, and it would be better having a MY_controller, a class previous to your controller class in which you'll write which footer you're referring:
Check for writing a MY_Controller here: http://ellislab.com/codeigniter/user-guide/general/core_classes.html
Then, write your MY_Controller:
class MY_Controller extends CI_Controller {
function __construct()
{
parent::__construct();
}
public function load_page( $page )
{
$data['main_body'] = $page;
// Logic for your header or footer deppending on logging or whatever...
if ( 1 ==1 ) {
$data['header'] = "templates/header.php";
$data['footer'] = "templates/footer.php";
}
$this->load->view('template.php',$data);
}
}
and you'll have to make sure your controllers extends MY_Controller class, and add a load_page method to whom you'll pass the argument:
class Custom_page extends MY_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
$this->load_page( 'dashboard_pages/dashboard_view' );
}
}
I think with that you'll have exactly what you look for, that way you only have to write in one place the logic for the header and footer: In MY_Controller, and just use it in any part.

CodeIgniter : Using both core classes and extended classes ?

I'm currently working on CI for my website, and i'm having some trouble about extending Controller_CI.
I have one controller that deals with login/signin actions, which doesn't need authentication, and others controllers that check if a user session exists before loading content.
For that purpose, I created MY_Controller class and add authentication code in the constructor.
Then I made all my controller extend MY_Controller, except the first one that still extends Controller_CI
My question is : Is it the right way to deals with authentication ? Is it still possible to use Controller_CI even if it's extended ?
I found another pattern :
http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY
I guess it's better, but still, I don't understand why not using the first solution.
Thanks
Extending controller class for that purpose will work, but this solution is not much flexible. I would rather create a library that handles authentication, and run it from a controller when it is desired. Please read http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html for details about creating custom libraries in CI.
Please remember you can only extend the CI_Controller with MY_Controller only once. In that aspects it's not a good idea. Suppose you want to implement another feature (e.g. a piece of code that makes a specific entry in the log) for some controllers, but not necessarily the controllers that need authentication you cannot make another MY_Controller.
Using a library is a better thing.
I'm using the flexi auth library on a big CI site. On every controller that requires authentication I just add the following:
public function __construct() {
parent::__construct();
$this->load->library('flexi_auth');
if (!$this->flexi_auth->is_logged_in())
redirect('auth/login');
}
I think a combination of what Phil Sturgeon suggests in that blog post and using a library would be best. So I would create a core controller (by that I mean a controller you place into application/core that extends CI_Controller) called MY_Controller which will look something like this
class MY_Controller extends CI_Controller
{
function __construct()
{
parent::__construct();
}
//Any other functions you want
}
Then judging by your question you currently have controllers that fit into two categories
Controllers that do require a logged in user before they do
anything
Controllers that don't require a logged in user before they do anything
So I would then create another controller in the /application/core directory that extends MY_Controller but in its constructor it checks to see if the user is logged in
class Auth_Controller extends My_Controller
{
function __construct()
{
parent::__construct();
//Check to see if the user is logged in
$this->load->library('authentication');
if(!$this->authentication->user_logged_in())
{
redirect('/login');
}
}
//Any other functions you want
}
Now when you create you controller you can choose which one of the core controllers you want to extend. If its a controller than doesn't require a logged in user you can extend MY_Controller but if it does required a logged in user you can extend Auth_Controller. That way it means you only need to do the user login check once in your code.
Like others have said if may be a good idea to place any authentication code into a library as that's a better place to put it than in a controller.
Summary
So to summarise by having all of your controllers extend core controllers rather than CI_Controller it should cut down on code repetition.
I also currently working on a CI project and had the same issue. I have came up with a different solution to deal with the authentication.
I extended the core controller as bellow,
class MY_Controller extends CI_Controller
{
public $data = array();
public $calledClass ;
public $calledMethod ;
public function __construct()
{
parent::__construct();
$authException['authentication']['login'] = true;
$authException['authentication']['logout'] = true;
$authException['welcome']['index'] = true;
$this->load->library("router");
$this->calledClass = $this->router->fetch_class();
$this->calledMethod = $this->router->fetch_method();
if(!#$authentication[$this->calledClass][$authentication->calledMethod] && !Auth::isUserLoggedIn())
{
# IS_AJAX is a contant defined in the contants.php
# define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
if(IS_AJAX)
{
# if this is an AJAX call, it sets a value in the header ,
# which can be captured in the AJAX response
# to redirect the user to the login page.
$this->output->set_header("auth-him:1");
exit;
}
else
{
redirect("authentication/login");
}
}
}
}
Hope the code above is clear and this helps.
To extend core controller more than one time, If you still need to use 2 controllers to handle authentication, I have followed this method
https://stackoverflow.com/a/22125436/567854
Hope this also helps :)

How to define a global variable(value) in codeIgniter

I simply do not mean how to define a global variable/constant in CodeIgniter.
Let me explain:
I have made a theme engine which is select-able from the current logged in user's control panel. This engine is not very complicated, but a simple folder. anyway, what I do across the application, is that I write one line of code to get the current theme selected by the user. I use a one line of code to get the name, and then store it in a variable:
$theme_name = $this->theme->get_theme_with_slash(false);
And then, I user $theme_name like this to get the proper view:
$this->load->view($theme_name.'result', $data);
And in all of my controllers that loads view I should repeat this process. What I need, is to call the function which gets the theme_name and store in the variable and then use the variable/session/function across the application. My approach currently is the helper's function which is a little less convenient compared to session/variable.
I got this from the manual and this what I have at the top of my config/config.php file: (i have a custom config set to paypal testing)
// HOW TO USE - For example if there's $config['foo'] = 'bar';
// in the config
// using $this- >config->item('foo') will be 'bar'.
// example for my paypal testing:
$config['paypaltest']=0;
http://ellislab.com/codeigniter%20/user-guide/libraries/config.html
and how to access in a controller:
$paypaltest = $this->config->item('paypaltest');
Create A core controller, since your process requires logical operations then you need a method for that.
application/core/MY_Controller.php
class MY_Controller Extends CI_Controller
{
protected $default_theme = 'theme';
public function __construct()
{
parent::__construct();
}
public function get_theme()
{
//your code for selecting the current theme selected from
//the database
$theme_from_db = '';
return $theme_from_db == NULL ? $this->default_theme : $theme_from_db;
}
}
Your Controller must extend MY_Controller
application/controller/view.php
class view extends MY_Controller
{
public function index()
{
$this->load->view($this->get_theme().'result', $data);
}
}
in code igniter global constants can be defined in
config->constants.php
even you no need to load it,it automatically autoloaded by CI automatically.
In Codeigniter all constant is defined inside application/config/constant.php.
like: define("CONSTANTNAME","value");
Constant degined here is accessible throughout all pages, ie; controllers, models and views

How do I extend the code igniter controller class?

In my CI system\libraries directory I have a new class named DD_Controller.php. This file looks like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class DD_Controller extends Controller
{
protected $ddauthentication;
function __construct()
{
parent::Controller();
$this->ddauthentication = "Authenticated";
}
}
?>
My application controller is defined like this:
class Inquiry extends DD_Controller
{...}
The Inquiry class works fine when I extend Controller, but I get a
Fatal error: Class 'DD_Controller' not
found in
C:\development\localhost\applications\inquiry\controllers\inquiry.php
on line 4
When I extend DD_Controller. In the config file I have the prefix defined as such:
$config['subclass_prefix'] = 'DD_';
Any idea of what I'm missing?
TIA
This is a better approach. Do the following:
Go to the following directory: your_ci_app/application/core/ and create a php file called MY_Controller.php (this file will be where your top parent classes will reside)
Open this the file you just created and add your multiple classes, like so:
class Admin_Parent extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function test() {
var_dump("from Admin_Parent");
}
}
class User_Parent extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function test(){
var_dump("from User_Parent");
}
}
Create your children controllers under this directory your_ci_app/application/controllers/ . I will call it adminchild.php
Open adminchild.php and create your controller code, make sure to extend the name of the parent class, like so:
class Adminchild extends Admin_Parent {
function __construct() {
parent::__construct();
}
function test() {
parent::test();
}
}
DD_Controller.php should be in /system/application/libraries/
If you're using the same CI for multiple apps, and you want them all to be able to extends their controllers to your custom one then you can extend the base Controller class in the same file.
In system/libraries/Controller.php below the Controller class:
class Mega_Controller extends Controller {
function Mega_Controller()
{
parent::Controller();
// anything you want to do in every controller, ye shall perform here.
}
}
Then you'll be able to do this in your app controllers:
class Home extends Mega_Controller {
....
Since the extended controller class you created will be available. I think this is better then overwriting the base controller, but that would work as well.
I recommend to avoid "cracking" CodeIgniter core files.
Better use its native extending possibilities and try to fit into them.
The same rule I would recommend for any PHP library / CMS.
This rule has few reasons:
- ability to quiclky upgrade without takint into account thousands of notes where and how was cracked in core files;
- portability;
- possibility to share your code - eg, this will be usable by both you and your friends in case of need, and it will help them to keep their library up to date, the same as you.
In other words, this is much more professional and it pays to you in the future by usability, portability and by update application possibility.
Regarding your personal question...
As for me, there is nothing bad to create your own library with everything you need to extend native CodeIgniter Controller, then load this library in Controller's constructor and you are done. The only thing to make better usability is to give short name to your library.
This way you can even divide what you need in different pieces and put into separate libraries:
WebFeatures
AdminFeatures
etc.
Then you just load needed libraries in your controller's constructor and you are done.
P.S. I know that proposed way does not fit into "right" OOP concept, but in the same time you must never forget about the integrity of the libraries used.
Everything above is just one more view of mine 7-years experience in professional web development, so I hope it will be helpful if not to follow, then at least to take into account.
Regards,
Anton

Categories