Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a concept of a middleware in CodeIgniter?

Tags:

codeigniter

I want to do some pre and post processing of requests like handling authentication, loading contextual data, performance timings and things like that. Coming from Django there's a concept of MIDDLEWARE_CLASSES that lets me handle the request in various stages: https://docs.djangoproject.com/en/dev/topics/http/middleware/

Currently it seems like each Controller has to do the same setup and load, in the constructor which isn't ideal because if the constructor fails, the class doesn't get initialized which has subtle but important consequences. I want to move this global handling to a global place.

Any suggestions?

like image 433
Kit Sunde Avatar asked Mar 18 '23 02:03

Kit Sunde


2 Answers

There is not. This might be helpful. Codeigniter forum

like image 169
Kit Sunde Avatar answered Apr 06 '23 00:04

Kit Sunde


You need to use hooks for this, Edit your application/config/hooks.php

$hook['post_controller_constructor'][] = array(
                                        'class'    => 'Autologin',  
                                        'function' => 'cookie_check', 
                                        'filename' => 'autologin.php',  
                                        'filepath' => 'hooks'
                                    );
like image 34
Irshad Khan Avatar answered Apr 06 '23 00:04

Irshad Khan