TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
These forums are read-only and for archival purposes only!
Please join our new forums at discourse.kohanaframework.org
after_filter and before_filter like Rails
  • this is my MY_Controller.php:

    class Controller extends Controller_Core {

    protected $before_filter = array();
    protected $after_filter = array();

    public function __construct()
    {
    parent:GDN__construct();

    /**
    * Getting the method of Router class
    */
    $uri_method = Router::$method;
    foreach($this->before_filter as $filter_method=>$filters){
    if(method_exists($this , $filter_method)){
    if( isset($filters['only']) AND $a_methods = $filters['only'] ){
    if( in_array($uri_method, $a_methods ) ){
    //add event after constructor and before call the method
    Event::add('system.post_controller_constructor', array($this, $filter_method));
    }
    }elseif( isset($filters['except']) AND $a_methods = $filters['except'] ){
    if( !in_array($uri_method, $a_methods ) ){
    //add event after constructor and before call the method
    Event::add('system.post_controller_constructor', array($this, $filter_method));
    }
    }
    }
    }

    foreach($this->after_filter as $filter_method=>$filters){
    if(method_exists($this , $met)){
    if( isset($filters['only']) AND $a_methods = $filters['only'] ){
    if( in_array($uri_method, $a_methods ) ){
    //add event after method execution
    Event::add('system.post_controller', array($this, $filter_method));
    }
    }elseif( isset($filters['except']) AND $a_methods = $filters['except'] ){
    if( !in_array($uri_method, $a_methods ) ){
    //add event after method execution
    Event::add('system.post_controller', array($this, $filter_method));
    }
    }
    }
    }
    }


    }


    how to use:

    class Post_Controller extends Controller {

    public function __construct()
    {
    //filter goes here before parent::construct();

    //call method 'is_logged' for all methods in the class, except 'show' method
    $this->before_filter['is_logged']['except'] = array('show');

    //call method 'audit' for only 'create' and 'update' methods
    $this->after_filter['audit']['only'] = array('create','update');

    parent:GDN__construct();
    }

    public function edit(){
    }
    public function update(){
    }
    public function create(){
    }
    public function show(){
    }

    protected function audit()
    {
    //example
    ORM::factory('Audit')->post_updated_or_created(date("Y-m-d"));
    }


    protected function is_logged()
    {
    //example
    if(!$this->auth->logged_in()){
    url::redirect('login');
    }
    }

    }



    syntax:
    $this->before_filter['filter_method']['only' or 'except'] = array('methods_to_filter');
    AND | OR
    $this->after_filter['filter_method']['only' or 'except'] = array('methods_to_filter');
  • Looks like a lot of magical code to "solve" a problem that can be solved in 2-3 lines of code.

  • An example of one of the reasons I left RoR development. "Magical" code just makes for convoluted code. Nice try though.
  • It's a good idea, it just doesn't mesh well with how things are done in Kohana. There's a general dislike for "magic".
  • before_filter: use "__construct() {}"
    after_filter: use "__destruct() {}"

    around_filter ???
  • Is there something similar in KO3.3 ?

    @shadowhand When you say "Looks like a lot of magical code to "solve" a problem that can be solved in 2-3 lines of code.". Can you give those 2-3 lines =)

  • public function before()
    {
        $this->auth = Auth::instance();
    
        // $this->before_filter['is_logged']['except'] = array('show');
        if ( ! $this->auth->logged_in() AND $this->request->action() !== 'show'))
        {
            HTTP::redirect(Route:url('login'));
        }
    }
    
  • How you'd do after filter?

  • C'mon is it really that hard?

    public function after()
    {
        if ($this->request->action() === 'create' or $this->request->action() === 'update')
        {
            ORM::factory('Audit')->post_updated_or_created(date("Y-m-d"));
        }
    }
    

    Note: Don't forget parent::before() or parent::after() if needed.

  • Kohana 2.3 didnt have before/after, post necro

  • @dyron Ok, I'll explain why Im here. I try to make this :

    1) ajax call POST comment

    2) comment/create RESTful service involved here that creates the comment and respond to the browser

    3) after it is done, make anything to call another php method without throwing another ajax call

    With RoR after_filter() it would be possible to return to the browser a response and then continue a process. Using after() or before() don't change anything cause it is include in the flow of the first ajax call and it runs before the response come back to the browser.

  • After reading a lot about this, the simplest way to do this would be using a queue like this https://github.com/jonnyjon/Task-Queue

    I'll go this way.

    Thank you for all your help.

  • @pocesar: Ko3.3 was requested.

    @flyder: You want to do a sub request? Wait. A php call after the response has send?

  • A php call after the response has send?

    This is what he wants to do. He should just use resque or something similar since it was designed for his problem.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion