`
xieye
  • 浏览: 803918 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

phalcon 自定义事件使用的多种方式

    博客分类:
  • PHP
阅读更多
方法1:官方文档的方式。

这是控制器:
    public function indexAction()
    {
        $this->view->disable();
        $eventsManager = new EventsManager();
        $myComponent = new A1();
        $eventsManager->attach('my-component', new SomeListen());
        $myComponent->setEventsManager($eventsManager);
        $myComponent->someTask();
    }

这是组件,
<?php

namespace Learn_phalcon;
use Phalcon\Events\EventsAwareInterface;
use Phalcon\Events\ManagerInterface;


class A1 implements EventsAwareInterface
{
    protected $eventsManager;

    public function setEventsManager(ManagerInterface $eventsManager)
    {
        $this->eventsManager = $eventsManager;
    }

    public function getEventsManager()
    {
        return $this->eventsManager;
    }

    public function getinfo(){
        return "is A1";
    }

    public function someTask()
    {
        $this->eventsManager->fire('my-component:beforeSomeTask', $this);
        echo 'Here, someTask<br>' ;
        $this->eventsManager->fire('my-component:afterSomeTask', $this);
    }
}

下面是监听器
<?php

namespace Learn_phalcon;

use Phalcon\Events\Event;

class SomeListen
{
    public function beforeSomeTask(Event $event, $myComponent)
    {
        echo "Here, beforeSomeTask". $myComponent->getinfo() ."<br>\n";
    }

    public function afterSomeTask(Event $event, $myComponent)
    {
        echo "Here, afterSomeTask". $myComponent->getinfo()."<br>\n" ;
    }
}

最终输出:
Here, beforeSomeTaskis A1
Here, someTask
Here, afterSomeTaskis A1


第2种方式,构造方法自定义:

监听器不变,控制器如下
public function index2Action()
    {
        $this->view->disable();
        $myComponent = new A2();
        $myComponent->someTask();
    }
组件A2.php如下:

<?php

namespace Learn_phalcon;
use Phalcon\Events\EventsAwareInterface;
use Phalcon\Events\ManagerInterface;
use Phalcon\Events\Manager as EventsManager;


/*
* 此类本身包含
*
*/
class A2 implements EventsAwareInterface
{
    public function __construct()
    {
        $eventsManager = new EventsManager();
        $eventsManager->attach('my-component', new SomeListen());
        $this->setEventsManager($eventsManager);
    }

    protected $eventsManager;

    public function setEventsManager(ManagerInterface $eventsManager)
    {
        $this->eventsManager = $eventsManager;
    }

    public function getinfo(){
        return "is A2";
    }

    public function getEventsManager()
    {
        return $this->eventsManager;
    }

    public function someTask()
    {
        $this->eventsManager->fire('my-component:beforeSomeTask', $this);
        echo 'A2 is Here, someTask<br>\n';
        $this->eventsManager->fire('my-component:afterSomeTask', $this);
    }
}

效果如下:
Here, beforeSomeTaskis A2
A2 is Here, someTask
\nHere, afterSomeTaskis A2

第3种方式,自定义容器方式:

监听器不变,控制器如下:
public function index3Action()
    {
        $this->view->disable();
        $myComponent = $this->getDi()->get('my_a3');
        $myComponent->someTask();
    }

组件和A1一样,只是类名改成A3:
此外,添加容器。 在项目初始化时。

$di->setShared('my_a3', function()use($di) {
    $a3 = new \Learn_phalcon\A3();
    $eventManager = new EventsManager();
    $eventManager->attach('my-component:beforeSomeTask' , new \Learn_phalcon\SomeListen() );
    $eventManager->attach('my-component:afterSomeTask' , new \Learn_phalcon\SomeListen() );
    $a3->setEventsManager($eventManager);
    return $a3;
});

效果如下:
Here, beforeSomeTaskis A3
A3 is Here, someTask
\nHere, afterSomeTaskis A3

总结
使用事件花样繁多,总有一款适合你。

另外,方式1 虽然最啰嗦,但有一个大好处,就是监听器可以获取当前运行环境的变量,放自己构造方法里。

而方式2 ,方式3,是预先定义的,于是没法写参数了。于是监听器就不好处理了。
只能放到组件里,可是这样,就有点耦合了。








0
0
分享到:
评论

相关推荐

    PhalconPHP 3.4.0.zip

    增加了侦听请求的功能:beforeAuthorizationResolve和request:afterAuthorizationResolve事件,这种能力可以使用自定义授权解析器#13327;在Phalcon\Mvc\Model中添加呼叫事件afterFetch:刷新#12220;添加Phalcon...

    PHP7框架Phalcon7 v1.2.3

    开发者不需要学习和使用 C 语言的功能, 因为所有的功能都以 PHP 类的方式暴露出来,可以直接使用。 Phalcon 也是松耦合的,可以根据项目的需要任意使用其他对象。Phalcon 不只是为了卓越的性能, 我们的目标是让它...

    phalcon框架的基本使用方法和phpunit测试

    phalcon框架的基本使用方法 phpunit测试框架的安装和测试

    php phalcon模型使用中文帮助文档

    Phalcon 是开源、全功能栈、使用 C 扩展编写、针对高性能优化的 PHP 5 框架。 开发者不需要学习和使用 C 语言的功能, 因为所有的功能都以 PHP 类的方式暴露出来,可以直接使用。 其中模型是我们开发中常用的,可...

    Phalcon 3.1.1 中文文档 chm

    开发者不需要学习和使用 C 语言的功能, 因为所有的功能都以 PHP 类的方式暴露出来,可以直接使用。 Phalcon 也是松耦合的,可以根据项目的需要任意使用其他对象。 Phalcon 不只是为了卓越的性能, 我们的目标是让...

    Phalcon php框架 v2.0.2.zip

    无需学习和使用 C 语言,所有函数都以 PHP 类的方式曾现。Phalcon 是一个松耦合的框架。   Phalcon 2.0 基于高性能编译语言Zephir,Zephir的目的是提供一个全新的更简单的语法来编写PHP扩展。   Phalcon 1.x还...

    phalcon开发工具 phalcon-tools

    phalcon开发工具 phalcon-tools

    phalcon中文开发文档

    开发者不需要学习和使用 C 语言的功能, 因为所有的功能都以 PHP 类的方式暴露出来,可以直接使用。 Phalcon 也是松耦合的,可以根据项目的需要任意使用其他对象。 Phalcon 不只是为了卓越的性能, 我们的目标是让...

    Phalcon7 (高性能 PHP 7框架) v1.2.3.zip

    开发者不需要学习和使用 C 语言的功能, 因为所有的功能都以 PHP 类的方式暴露出来,可以直接使用。 Phalcon 也是松耦合的,可以根据项目的需要任意使用其他对象。 Phalcon7 (高性能 PHP 7框架) v1.2.3更新日志 ...

    phalcon 中文文档 全中文手册

    phalcon 中文文档 全中文手册 网页格式 放在环境中 或直接打开查看

    Phalcon 3.0.0 中文文档

    Phalcon 3.0.0 中文文档 Phalcon 3.0.0 中文文档 Phalcon 3.0.0 中文文档

    PHP扩展Phalcon源码

    Phalcon is optimized for high performance. Its unique architecture allows the framework to always be memory resident, offering its functionality whenever its needed, without expensive file stats and ...

    phalcon IDE代码提示库

    phalcon 框架 IDE代码提示 phpstrom NetBeans 等IDE 使用的代码提示

    Phalcon MVC项目源码

    基于Phalcon的mvc项目配置,本资源中包含多种不同风格的项目配置, 有;color:red;"&gt;8种基于不同目录结构的mvc配置, 适合初学者参考

    Phalcon的PhpStorm代码提示说明

    Phalcon的PhpStorm代码提示说明 Phalcon的PhpStorm代码提示说明

    phalcon_2.0_x64

    phalcon 2.0.0版本64位 包含PHP5.3、PHP5.4、PHP5.5、PHP5.6 本资料共包含以下附件: phalcon_x64_VC9_php5.3.9_2.0.0.zip phalcon_x64_VC9_php5.3.9_2.0.0_nts.zip phalcon_x64_VC9_php5.4.0_2.0.0.zip phalcon...

    phalcon框架教程

    同时Phalcon是松耦合的,您可以根据需要使用其他组件。 Phalcon不只是性能优越,我们的目标是让它强大而且易于使用! 一般都是直接看英文资料,大都能看得懂。但看到Phalcon框架如此优秀,在学习后就想和大家分享,...

    Phalcon Docs Mobi 格式文档

    Phalcon 官方最新版 v3.3 的mobi格式的文档: Phalcon is an open source full stack framework for PHP, written as a C-extension. Phalcon is optimized for high performance. Its unique architecture allows ...

    phalcon框架的demo

    phalcon框架的demo

    Phalcon 2.0.10 文档 html中文版

    Phalcon 2.0.10 文档 html中文版。Phalcon 框架, 一种崭新的 PHP 框架,目前已经3.x了,这里提供的是2.0.10的文档,建议PHP5.5,5.6使用,

Global site tag (gtag.js) - Google Analytics