用户组件类(User Component Class)

Phalcon 提供许多类来简化常见编码,如对文本或数组的操作, HTML 标签生成等等。

组件类一览(Overview)

组件类列表(Component Class List)

名称 描述
插件类 Phalcon\Mvc\User\Plugin
组件类 Phalcon\Mvc\User\Component
模块类 Phalcon\Mvc\User\Module
逻辑类 Phalcon\Mvc\User\Logic
逻辑模型类 Phalcon\Mvc\User\Logic\Model

插件类(Plugin Class)

一般用于事件的拦截处理,例如权限检测插件 SecurityPlugin

  1. <?php
  2.  
  3. use Phalcon\Acl;
  4. use Phalcon\Events\Event;
  5. use Phalcon\Mvc\User\Plugin;
  6.  
  7. class SecurityPlugin extends Plugin
  8. {
  9. public function isAllowed($role, $controller, $action)
  10. {
  11. // ...
  12. }
  13.  
  14. public function beforeExecuteRoute(Phalcon\Events\Event $event, Phalcon\Mvc\Dispatcher $dispatcher)
  15. {
  16. $auth = $this->session->get('auth');
  17. if (!$auth) {
  18. $role = 'Guests';
  19. } else {
  20. $role = 'Users';
  21. }
  22.  
  23. $controller = $dispatcher->getControllerName();
  24. $action = $dispatcher->getActionName();
  25.  
  26. $allowed = $this->isAllowed($role, $controller, $action);
  27. if ($allowed != Acl::ALLOW) {
  28.  
  29. $this->flash->error("You don't have access to this module");
  30. $dispatcher->forward(
  31. array(
  32. 'controller' => 'index',
  33. 'action' => 'index'
  34. )
  35. );
  36.  
  37. return false;
  38. }
  39. }
  40. }
  1. <?php
  2.  
  3. $di->set('dispatcher', function () {
  4. $eventsManager = new EventsManager();
  5. $eventsManager->attach('dispatch:beforeExecuteRoute', new SecurityPlugin);
  6.  
  7. $dispatcher = new Dispatcher();
  8. $dispatcher->setEventsManager($eventsManager);
  9. return $dispatcher;
  10. });

组件类(Component Class)

一般用于对框架功能的扩展。

模块类(Module Class)

一般用于对框架功能的扩展。

逻辑类(Logic Class)

一般用于处理业务逻辑,可以设置调度器绑定逻辑类,会根据控制器方法参数自动调用逻辑类静态方法 call 完成实例化,然后调用 start 完成初始化操作,当控制器方法执行结束后,将调用 finish 方法:

  1. <?php
  2.  
  3. $di->set('dispatcher', function () {
  4. $dispatcher = new Dispatcher();
  5. $dispatcher->setLogicBinding(true);
  6. return $dispatcher;
  7. });

控制器实现:

  1. <?php
  2.  
  3. class LogicController extends Phalcon\Mvc\Controller
  4. {
  5. public function indexAction(\MyLogic $logic)
  6. {
  7. // ...
  8. }
  9. }

逻辑类实现:

  1. <?php
  2.  
  3. class MyLogic extends Phalcon\Mvc\User\Logic
  4. {
  5. public $num = 0;
  6.  
  7. public function start()
  8. {
  9. // ...
  10. }
  11.  
  12. public function finish()
  13. {
  14. // ...
  15. $this->view->data = $this->getContent();
  16. }
  17.  
  18. // 该方法可以不实现
  19. public static function call($action = NULL, $params = NULL)
  20. {
  21. $logic = new MyLogic($action, $params);
  22. $logic->num = 1;
  23. return $logic;
  24. }
  25. }

逻辑模型类(Logic Model Class)

使用方式与逻辑类相同,比逻辑类多了几个预设的抽象方法,示例如下:

  1. class Mylogic extends Phalcon\Mvc\User\Logic\Model {
  2. public function get($arguments = NULL){}
  3. public function getAll($arguments = NULL){}
  4. public function save($arguments = NULL){}
  5. public function create($arguments = NULL){}
  6. public function delete($arguments = NULL){}
  7. public function deleteAll($arguments = NULL){}
  8. public function update($arguments = NULL){}
  9. public function updateAll($arguments = NULL){}
  10. public function count($arguments = NULL){}
  11. }

原文: http://www.myleftstudio.com/reference/user.html