![]() |
rails-like before_filter - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22) +--- Thread: rails-like before_filter (/showthread.php?tid=7421) |
rails-like before_filter - El Forum - 04-08-2008 [eluser]dioony[/eluser] Hi there! Now i want to thank you all for the great ideas and solutions and want to share my code with you too. I was searching for a filter-mechanism like in ruby on rails, but i only found the filter-system in the wiki. But i think that solution is to huge and could be more easier. so i written my own solution. Because there are some more requests for an rails-like-before-filter, i thought, it would be nice to share this. my solution exists only of one hook-class. 1. Configure the Hooks-Config first of all you have to configure the application/config/hooks.php. Add the following code : Code: $hook['post_controller_constructor'][] = array( 2. Add the Filter-Class add a new file to your CI-App called: application/hooks/Filter.php add the following code into this file: Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 3.Setting up the Controller Now you can set up you're controller to run the before_filter. Code: <? now, it is also possible to exclude some functions to be filtered. (e.g. if you want to redirect to the same controller) Code: var $before_filter = array('name' => 'authorization', 'except' => array('index')); another way is to specify only the functions which have to be filterd Code: var $before_filter = array('name' => 'authorization', 'only' => array('index')); 4. Annotations and Thoughts This solution only accepts one filter per controller. there are two reasons: 1. i think, that there should not be more than one filter to keep the code clear 2. i never needed more than one in rails or ci. But i think, it should be very easy to change it to a multiple-filter-hook. if someone take an interest in it, i could change the code. in rails you have to put the before_filter-method in the application-controller. in theory it should also be possible with CI, if you have written an application-controller and each of your controllers extends this one. Then you can put the filter-method into the application-controller. But i didn't test it! it should also be possible to write an after_filter. you only have to replace "post_controller_constructor" with "post_controller" in the hooks.php and change the variable "before_filter" to "after_filter" to keep the code clear. if you have any feedback, comments, bugs or something like that, feel free to reply rails-like before_filter - El Forum - 04-08-2008 [eluser]dioony[/eluser] i forgotten to notice the following: note that the hooks-config uses post_controller_constructor instead of pre_controller. this is necessary, because it uses some functions/attributes of the controller itself. therefore please note that the constructor is called before the filter will be called to avoid bugs! so, i you're using this filter, then never run any code in the constructor which normally have to run after the filter! rails-like before_filter - El Forum - 04-08-2008 [eluser]webthink[/eluser] Perhaps it's because I'm not familiar with rails but what advantage do filters give you over something like the following? Code: <? rails-like before_filter - El Forum - 04-09-2008 [eluser]dioony[/eluser] maybe your solution may work if you want to filter all the functions. the main idea was to create a filter, where you can decide which functions will be filtered and which not (except and only mechanism). i dont know how php or ci works, but if the constructor will only be called once for a session and not for each request, the authorization will only work for the first function-call and not for following function-calls. but i never dealt with something like that before... rails-like before_filter - El Forum - 07-03-2008 [eluser]sophistry[/eluser] xwero just showed how to do this by modifying the CI core... another approach to protecting controllers and methods and worth looking at for comparison and learning. protecting controllers and methods |