├── README.md ├── dist └── application │ └── core │ └── MY_Controller.php └── example └── application ├── controllers └── Home.php ├── core └── MY_Controller.php └── middlewares └── AdminAuthMiddleware.php /README.md: -------------------------------------------------------------------------------- 1 | # Codeigniter Middlewares 2 | 3 | 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. 4 | Tested on CodeIgniter 3.0.4, should work on 2.2+ as well 5 | 6 | #### Update: Still working flawlessly as of May 2019 7 | 8 | ### Quick Integration Guide 9 | 10 | * Copy MY_Controller.php to application/core 11 | * In your controller extend MY_Controller instead of CI_Controller 12 | * Create your middleware class in middleware directory and add function run() 13 | * Create a function middleware() and return array of middlewares to run 14 | * That's it. 15 | 16 | 17 | ##### Create your middlewares directory 18 | * Create new **middlewares** directory in application if not exists. 19 | 20 | ##### Create your class in application/middlewares 21 | 22 | ```php 23 | controller = $controller; 42 | $this->ci = $ci; 43 | } 44 | 45 | // This function is required, and is entry point to this class 46 | public function run(){ 47 | // you can reference to current controller called class 48 | $this->controller->some_your_method(); 49 | 50 | // you can run db queries 51 | $categories = $this->ci->db->get('categories'); 52 | 53 | // you can get reference to models, libraries 54 | $users = $this->controller->user->list(); 55 | $this->controller->load->library('session'); 56 | 57 | // you can get session references 58 | $email = $this->ci->session->userdata('email'); 59 | 60 | // you can modify the class and add your methods to this class 61 | $this->roles = array('somehting', 'view', 'edit'); 62 | 63 | // you can get reference to called function and class name on request 64 | $this->controller->router->method; // returns method name, eg. index 65 | $this->controller->router->class; // returns from which class (controller class) this function has been called 66 | 67 | // and also you can terminate the request, if you dont want it to pass on 68 | show_error('You are not allowed to perform this operation'); 69 | } 70 | } 71 | ``` 72 | 73 | ##### Extend MY_Controller class 74 | ```php 75 | middlewares['admin_auth']); 101 | 102 | $this->load->view('index'); 103 | } 104 | 105 | // Middlewares applied according to above code: admin_auth, someother 106 | public function posts() 107 | { 108 | $this->load->view('posts_view'); 109 | } 110 | 111 | // Middlewares applied according to above code: admin_auth 112 | public function list() 113 | { 114 | $this->load->view('something'); 115 | } 116 | } 117 | ``` 118 | 119 | #### Notes: 120 | 121 | > 122 | > Class name require **Middleware** as suffix, also cannot contain, underscores or hyphens 123 | > Here is list of some valid conventions: 124 | > 125 | 126 | * admin => AdminMiddleware 127 | * Admin => AdminMiddleware 128 | * SomeThing => SomeThingMiddleware 129 | * some_lazy_name => SomeLazyNameMiddleware 130 | * some_OtHer_Crazy_name => SomeOtHerCrazyNameMiddleware 131 | * hell_Yeah => HellYeahMiddleware 132 | 133 | On the left side is name of middleware, and on the right side is class name and .php filename for the class. 134 | Above list explains, how your middleware name would resolve to a class name. 135 | 136 | That's all, I hope documentation and code was helpful. Cheers! 137 | -------------------------------------------------------------------------------- /dist/application/core/MY_Controller.php: -------------------------------------------------------------------------------- 1 | _run_middlewares(); 14 | } 15 | 16 | protected function middleware(){ 17 | return array(); 18 | } 19 | 20 | protected function _run_middlewares(){ 21 | $this->load->helper('inflector'); 22 | $middlewares = $this->middleware(); 23 | foreach($middlewares as $middleware){ 24 | $middlewareArray = explode('|', str_replace(' ', '', $middleware)); 25 | $middlewareName = $middlewareArray[0]; 26 | $runMiddleware = true; 27 | if(isset($middlewareArray[1])){ 28 | $options = explode(':', $middlewareArray[1]); 29 | $type = $options[0]; 30 | $methods = explode(',', $options[1]); 31 | if ($type == 'except') { 32 | if (in_array($this->router->method, $methods)) { 33 | $runMiddleware = false; 34 | } 35 | } else if ($type == 'only') { 36 | if (!in_array($this->router->method, $methods)) { 37 | $runMiddleware = false; 38 | } 39 | } 40 | } 41 | $filename = ucfirst(camelize($middlewareName)) . 'Middleware'; 42 | if ($runMiddleware == true) { 43 | if (file_exists(APPPATH . 'middlewares/' . $filename . '.php')) { 44 | require APPPATH . 'middlewares/' . $filename . '.php'; 45 | $ci = &get_instance(); 46 | $object = new $filename($this, $ci); 47 | $object->run(); 48 | $this->middlewares[$middlewareName] = $object; 49 | } else { 50 | if (ENVIRONMENT == 'development') { 51 | show_error('Unable to load middleware: ' . $filename . '.php'); 52 | } else { 53 | show_error('Sorry something went wrong.'); 54 | } 55 | } 56 | } 57 | 58 | } 59 | } 60 | } -------------------------------------------------------------------------------- /example/application/controllers/Home.php: -------------------------------------------------------------------------------- 1 | middlewares['admin_auth']->roles); 18 | } 19 | } -------------------------------------------------------------------------------- /example/application/core/MY_Controller.php: -------------------------------------------------------------------------------- 1 | _run_middlewares(); 14 | } 15 | 16 | protected function middleware(){ 17 | return array(); 18 | } 19 | 20 | protected function _run_middlewares(){ 21 | $this->load->helper('inflector'); 22 | $middlewares = $this->middleware(); 23 | foreach($middlewares as $middleware){ 24 | $middlewareArray = explode('|', str_replace(' ', '', $middleware)); 25 | $middlewareName = $middlewareArray[0]; 26 | $runMiddleware = true; 27 | if(isset($middlewareArray[1])){ 28 | $options = explode(':', $middlewareArray[1]); 29 | $type = $options[0]; 30 | $methods = explode(',', $options[1]); 31 | if ($type == 'except') { 32 | if (in_array($this->router->method, $methods)) { 33 | $runMiddleware = false; 34 | } 35 | } else if ($type == 'only') { 36 | if (!in_array($this->router->method, $methods)) { 37 | $runMiddleware = false; 38 | } 39 | } 40 | } 41 | $filename = ucfirst(camelize($middlewareName)) . 'Middleware'; 42 | if ($runMiddleware == true) { 43 | if (file_exists(APPPATH . 'middlewares/' . $filename . '.php')) { 44 | require APPPATH . 'middlewares/' . $filename . '.php'; 45 | $ci = &get_instance(); 46 | $object = new $filename($this, $ci); 47 | $object->run(); 48 | $this->middlewares[$middlewareName] = $object; 49 | } else { 50 | if (ENVIRONMENT == 'development') { 51 | show_error('Unable to load middleware: ' . $filename . '.php'); 52 | } else { 53 | show_error('Sorry something went wrong.'); 54 | } 55 | } 56 | } 57 | 58 | } 59 | } 60 | } -------------------------------------------------------------------------------- /example/application/middlewares/AdminAuthMiddleware.php: -------------------------------------------------------------------------------- 1 | controller = $controller; 15 | $this->ci = $ci; 16 | } 17 | 18 | public function run(){ 19 | $this->roles = array('somehting', 'view', 'edit'); 20 | } 21 | } --------------------------------------------------------------------------------