- 浏览: 803918 次
- 性别:
- 来自: 南京
文章分类
最新评论
-
xieye:
jetty插件有好几个版本,1.6,1.7,1.8
我选的是用 ...
tapestry入门(翻译)3 导入项目到eclipse -
xieye:
还有,
注:第2部分时,需要先安装jetty,我自己在安装过程 ...
tapestry入门(翻译)3 导入项目到eclipse -
xieye:
说明一下:实际使用中,导入时我并没有错误。2、我把eclips ...
tapestry入门(翻译)3 导入项目到eclipse -
xieye:
其实还是有一些先决条件的。1是外部环境,2是进步是阶段性的(意 ...
(转载文章)如何愉悦起来:一位精神治疗师的见解 -
mandy_yanzi:
我都已经饿7天了坚持为了我的衣衣
身体健康的问题
方法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,是预先定义的,于是没法写参数了。于是监听器就不好处理了。
只能放到组件里,可是这样,就有点耦合了。
发表评论
-
召唤神龙 - 安装 centos 8, php 8, mysql 8, laravel 8 的整套 php 运行环境
2020-11-27 16:26 552本文编写时间:2020-11-27 传说集齐龙珠可以召唤神 ... -
centos8 使用阿里镜像快速安装php7.4套件
2020-11-15 00:48 662centos8 使用阿里镜像快速安装php7.4套件 本文只 ... -
centos7 使用阿里镜像快速安装php7.4套件
2020-11-15 00:32 999本文只考虑centos7 今日 2020-11-16 rem ... -
composer常用命令
2020-11-05 14:46 1002列出当前全局配置 composer config -l -g ... -
期待centos8+php8 + mysql8 + laravel8,8888
2020-10-29 19:24 378期待centos8+php8 + mysql8 目前主要使用 ... -
ubuntu 安装 php7.4以及各类插件
2020-10-24 13:20 894ubuntu 安装 php7.4以及各类插件 安装系统 最近 ... -
宝塔定义站点
2020-02-24 10:41 2宝塔定义站点。 1111111111111111111111 ... -
让php-fpm以root账号运行
2020-02-19 21:41 689一般来说,我们没有这种需求,但是当需要时, 可以让php-fp ... -
将代码部署到coding.net
2020-01-10 14:20 536coding.net 是一个git仓库,有免费有收费。 首先 ... -
php部署工具deployer技巧 - vendor目录创建
2020-01-06 12:06 801deployer是个好东西,可是每次都composer ,其实 ... -
自定义redis多个实例
2020-01-04 01:17 577假如多个项目部署到同一台服务器,有时会被redis的各项目前缀 ... -
推荐使用php的部署工具 deployer
2020-01-03 16:48 805php部署代码可以使用 https://deployer.or ... -
使用composer 类库 cachetool 清除opcache 的缓存
2020-01-03 16:38 875php的生产服务器上,需要把opche打开。 但有时,会被缓 ... -
laravel容器学习体会
2019-12-31 15:11 565根据文档与实测结果 假设我在app下建一个Services目 ... -
phalcon 自定义超强日志类
2019-12-18 10:00 759phalcon自带的日志文件类,有两个麻烦之处。 1、路径不能 ... -
自定义后台的使用
2019-12-18 05:22 359如果自己写后台,保存用户的表单请求值 所有的情况如下: 1 ... -
phalcon对控制器返回值的使用以输出json
2019-12-16 19:17 728在phalcon里,控制器的Action的方法的返回值没啥用。 ... -
phalcon数据库DB使用实例
2019-12-12 17:12 586整理了一下phalcon的db使用,写了个demo。 ... -
php-redis5.0降级成php-redis4.3
2019-08-22 00:01 832最新版本的php的redis插件,版本是5.0 但是可能会有 ... -
aes加密解密php和java互通代码
2019-08-20 20:19 1078代码主要来源如下,但是注意做了修改: https://gist ...
相关推荐
增加了侦听请求的功能:beforeAuthorizationResolve和request:afterAuthorizationResolve事件,这种能力可以使用自定义授权解析器#13327;在Phalcon\Mvc\Model中添加呼叫事件afterFetch:刷新#12220;添加Phalcon...
开发者不需要学习和使用 C 语言的功能, 因为所有的功能都以 PHP 类的方式暴露出来,可以直接使用。 Phalcon 也是松耦合的,可以根据项目的需要任意使用其他对象。Phalcon 不只是为了卓越的性能, 我们的目标是让它...
phalcon框架的基本使用方法 phpunit测试框架的安装和测试
Phalcon 是开源、全功能栈、使用 C 扩展编写、针对高性能优化的 PHP 5 框架。 开发者不需要学习和使用 C 语言的功能, 因为所有的功能都以 PHP 类的方式暴露出来,可以直接使用。 其中模型是我们开发中常用的,可...
开发者不需要学习和使用 C 语言的功能, 因为所有的功能都以 PHP 类的方式暴露出来,可以直接使用。 Phalcon 也是松耦合的,可以根据项目的需要任意使用其他对象。 Phalcon 不只是为了卓越的性能, 我们的目标是让...
无需学习和使用 C 语言,所有函数都以 PHP 类的方式曾现。Phalcon 是一个松耦合的框架。 Phalcon 2.0 基于高性能编译语言Zephir,Zephir的目的是提供一个全新的更简单的语法来编写PHP扩展。 Phalcon 1.x还...
phalcon开发工具 phalcon-tools
开发者不需要学习和使用 C 语言的功能, 因为所有的功能都以 PHP 类的方式暴露出来,可以直接使用。 Phalcon 也是松耦合的,可以根据项目的需要任意使用其他对象。 Phalcon 不只是为了卓越的性能, 我们的目标是让...
开发者不需要学习和使用 C 语言的功能, 因为所有的功能都以 PHP 类的方式暴露出来,可以直接使用。 Phalcon 也是松耦合的,可以根据项目的需要任意使用其他对象。 Phalcon7 (高性能 PHP 7框架) v1.2.3更新日志 ...
phalcon 中文文档 全中文手册 网页格式 放在环境中 或直接打开查看
Phalcon 3.0.0 中文文档 Phalcon 3.0.0 中文文档 Phalcon 3.0.0 中文文档
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代码提示 phpstrom NetBeans 等IDE 使用的代码提示
基于Phalcon的mvc项目配置,本资源中包含多种不同风格的项目配置, 有;color:red;">8种基于不同目录结构的mvc配置, 适合初学者参考
Phalcon的PhpStorm代码提示说明 Phalcon的PhpStorm代码提示说明
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 官方最新版 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 2.0.10 文档 html中文版。Phalcon 框架, 一种崭新的 PHP 框架,目前已经3.x了,这里提供的是2.0.10的文档,建议PHP5.5,5.6使用,