Skip to content

Instantly share code, notes, and snippets.

@etaubman
Last active December 16, 2015 05:08
Show Gist options
  • Save etaubman/5381798 to your computer and use it in GitHub Desktop.
Save etaubman/5381798 to your computer and use it in GitHub Desktop.
A method for extending CodeIgniter to have pre and post filters simliar to RoR and Cake. The actual method call is left in for reference. This is and addition to the file ./system/core/CodeIgniter.php
<?php //this is a snippet from ./system/core/CodeIgniter.php
/*
* ------------------------------------------------------
* Is there a pre-filter on the requested method?
* ------------------------------------------------------
*/
if (isset($CI->pre_filter) && is_array($CI->pre_filter))
{
foreach ($CI->pre_filter as $pre_filter_action => $filtered_action)
{
if (is_string($filtered_action) && $method == $filtered_action)
{
//call pre-filter
call_user_func_array(array(&$CI,$pre_filter_action),array_slice($URI->rsegments, 2));
}
else if (is_array($filtered_action) && in_array($method,$filtered_action))
{
//call pre-filter
call_user_func_array(array(&$CI,$pre_filter_action),array_slice($URI->rsegments, 2));
}
}
}
/*
* ------------------------------------------------------
* Call the requested method
* ------------------------------------------------------
*/
//LEAVE THE STANDARD CI METHOD CALL IN HERE
/*
* ------------------------------------------------------
* Is there a post-filter on the requested method?
* ------------------------------------------------------
*/
if (isset($CI->post_filter) && is_array($CI->post_filter))
{
foreach ($CI->post_filter as $post_filter_action => $filtered_action)
{
if (is_string($filtered_action) && $method == $filtered_action)
{
//call post-filter
call_user_func_array(array(&$CI,$post_filter_action),array_slice($URI->rsegments, 2));
}
else if (is_array($filtered_action) && in_array($method,$filtered_action))
{
//call post-filter
call_user_func_array(array(&$CI,$post_filter_action),array_slice($URI->rsegments, 2));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment