phalcon 自定义事件使用的多种方式
程序员文章站
2022-05-31 10:07:20
...
方法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,是预先定义的,于是没法写参数了。于是监听器就不好处理了。
只能放到组件里,可是这样,就有点耦合了。
这是控制器:
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,是预先定义的,于是没法写参数了。于是监听器就不好处理了。
只能放到组件里,可是这样,就有点耦合了。
推荐阅读
-
JavaScript中多种绑定事件方式的区别讲解
-
详解 Android中Libgdx使用ShapeRenderer自定义Actor解决无法接收到Touch事件的问题
-
wxpython中自定义事件的实现与使用方法分析
-
详解 Android中Libgdx使用ShapeRenderer自定义Actor解决无法接收到Touch事件的问题
-
wxpython中自定义事件的实现与使用方法分析
-
Mysql count 的多种使用方式性能比较
-
vue使用自定义事件的表单输入组件用法详解【日期组件与货币组件】
-
WPS表格单元格格式使用技巧自定义多种多样的格式
-
jackson在springboot中的使用方式-自定义参数转换器
-
spring boot之使用spring data jpa的自定义sql方式