Framework feature: before filters

Development related discussion.
Post Reply
User avatar
Jack
Advanced Member
Posts: 668
Joined: Fri Mar 27, 2009 7:37 pm
Location: Australia

Framework feature: before filters

Post by Jack » Tue Sep 27, 2011 11:40 pm

A new feature I just worked into the framework a few minutes ago is before filters.



Right now they are currently working on controllers and will soon be working for the model class also.



What does this mean and why is it an awesome thing? Well, let's say in our Tickets controller we have the new ticket action.



Instead of having to do something like this:


Code: Select all

public function action_new()

{

    if (current_user()->can_perform('create_ticket')) {

        // New ticket code

    } else {

        $this->no_permission();

    }

} 


We can do:


Code: Select all


public $_before = array('new' => '_check_permission');



public function action_new()

{

    // New ticket code

}



public function _check_permission()

{

    if (Router::$method == 'new' and !$this->user->can_perform('create_ticket')) {

        $this->no_permission();

    }

}

 


What we can also do is assign that same _check_permission() method to as many actions in that controller as we like and just use the if statement or a switch to check what action we're running and if the user has permission to execute [/code]that action.
Last edited by Jack on Tue Sep 27, 2011 11:41 pm, edited 1 time in total.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest