GithubHelp home page GithubHelp logo

nian88 / codeigniter-middleware Goto Github PK

View Code? Open in Web Editor NEW

This project forked from davinder17s/codeigniter-middleware

0.0 2.0 0.0 4 KB

Simplest yet powerful middleware library for codeigniter, can be used to make routes login only, apply roles and permission system, modify, intercept or preprocess requests.

PHP 100.00%

codeigniter-middleware's Introduction

Codeigniter Middlewares

This library enables you to quickly add any middleware to your codeigniter application and in too few lines you get everything up and running smoothly. Tested on CodeIgniter 3.0.4, should work on 2.2+ as well

Update: Still working flawlessly as of May 2017

Quick Integration Guide

  • Copy MY_Controller.php to application/core
  • In your controller extend MY_Controller instead of CI_Controller
  • Create your middleware class in middleware directory and add function run()
  • Create a function middleware() and return array of middlewares to run
  • That's it.
Create your middlewares directory
  • Create new middlewares directory in application if not exists.
Create your class in application/middlewares
<?php
// src: application/middlewares/AdminAuthMiddleware.php

// Extends nothing, it's upto you what you want to extend. Completely generic.
class AdminAuthMiddleware {
    // Get injected controller and ci references
    protected $controller;
    protected $ci;
    
    // Some custom and example data related to this class
    public $roles = array();
    
    // All middlewares will pass controller and ci class objects as references to constructor
    // It's upto you, that what you do with them
    // Obviously it's not required :)
    
    public function __construct($controller, $ci)
    {
        $this->controller = $controller;
        $this->ci = $ci;
    }
    
    // This function is required, and is entry point to this class
    public function run(){
        // you can reference to current controller called class
        $this->controller->some_your_method();
        
        // you can run db queries
        $categories = $this->ci->db->get('categories');
        
        // you can get reference to models, libraries
        $users = $this->controller->user->list();
        $this->controller->load->library('session');
        
        // you can get session references
        $email = $this->ci->session->userdata('email');
    
        // you can modify the class and add your methods to this class
        $this->roles = array('somehting', 'view', 'edit');
        
        // you can get reference to called function and class name on request
        $this->controller->router->method; // returns method name, eg. index
        $this->controller->router->class; // returns from which class (controller class) this function has been called
        
        // and also you can terminate the request, if you dont want it to pass on
        show_error('You are not allowed to perform this operation');
    }
}
Extend MY_Controller class
<?php
// src: application/controllers/Home.php

class Home extends MY_Controller 
{
    // only create if you want to use, not compulsory.
    // or return parent::middleware(); if you want to keep.
    // or return empty array() and no middleware will run.
    protected function middleware()
    {
        /**
         * Return the list of middlewares you want to be applied,
         * Here is list of some valid options
         *
         * admin_auth                    // As used below, simplest, will be applied to all
         * someother|except:index,list   // This will be only applied to posts()
         * yet_another_one|only:index    // This will be only applied to index()
         **/
        return array('admin_auth', 'someother|except:index,list', 'yet_another_one|only:index');
    }
    
    // Middlewares applied according to above code: admin_auth, yet_another_one
    public function index()
    {
        // you can also use the middleware class's object later if you wish.
        var_dump($this->middlewares['admin_auth']);
        
        $this->load->view('index');
    }
    
    // Middlewares applied according to above code: admin_auth, someother
    public function posts()
    {
        $this->load->view('posts_view');
    }
    
    // Middlewares applied according to above code: admin_auth
    public function list()
    {
        $this->load->view('something');
    }
}

Notes:

Class name require Middleware as suffix, also cannot contain, underscores or hyphens Here is list of some valid conventions:

  • admin => AdminMiddleware
  • Admin => AdminMiddleware
  • SomeThing => SomeThingMiddleware
  • some_lazy_name => SomeLazyNameMiddleware
  • some_OtHer_Crazy_name => SomeOtHerCrazyNameMiddleware
  • hell_Yeah => HellYeahMiddleware

On the left side is name of middleware, and on the right side is class name and .php filename for the class. Above list explains, how your middleware name would resolve to a class name.

That's all, I hope documentation and code was helpful. Cheers!

codeigniter-middleware's People

Contributors

davinder17s avatar

Watchers

James Cloos avatar Azhar nian avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.