Skip to content

Instantly share code, notes, and snippets.

@anhtuank7c
Created November 6, 2015 09:44
Show Gist options
  • Save anhtuank7c/a943d159841d9535cef5 to your computer and use it in GitHub Desktop.
Save anhtuank7c/a943d159841d9535cef5 to your computer and use it in GitHub Desktop.
<?php
namespace App\Controller;
use App\Event\CheckProfileListener;
use Cake\Event\Event;
/**
* CheckProfile trait
*/
trait CheckProfile {
public function beforeFilter(Event $event) {
parent::beforeFilter($event);
$this->eventManager()->on(new CheckProfileListener($this->Auth));
}
}
@hmic
Copy link

hmic commented Nov 6, 2015

CheckProfileTrait.php:

'myBeforeFilter']; } ``` public function myBeforeFilter() { throw new \Cake\Network\Exception\NotFoundException('hahaha'); } ``` } DashboardController.php:

@anhtuank7c
Copy link
Author

<?php

namespace App\Controller;

/**
 * CheckProfile trait
 */
trait CheckProfileTrait {

    public function implementedEvents() {
        return ['Controller.beforeFilter' => 'myBeforeFilter'];
    }

    public function myBeforeFilter() {
        throw new \Cake\Network\Exception\NotFoundException('hahaha');
    }

}

And

<?php

namespace App\Controller\Admin;

use App\Controller\AppController;
use App\Controller\CheckProfileTrait;
use Cake\Event\EventListenerInterface;

/**
 * Dashboard Controller
 *
 * @property \App\Model\Table\DashboardTable $Dashboard
 */
class DashboardController extends AppController implements EventListenerInterface {

    use CheckProfileTrait;

    /**
     * Index method
     *
     * @return void
     */
    public function index() {

    }

}

But cannot throw Exception

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment