Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/davinder17s/codeigniter-middleware

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.
https://github.com/davinder17s/codeigniter-middleware

codeigniter codeigniter-middlewares middleware php

Last synced: 3 months ago
JSON representation

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.

Awesome Lists containing this project

README

        

# 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 2019

### 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
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
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!