在使用Lumen进行开发的时候。如果你使用:./artisan的时候报错:Cannot declare class Event, because the name is already in use。检查一下LOG发现是:
XML/HTML代码
- [2016-12-07 15:27:37] lumen.ERROR: ErrorException: Cannot declare class Event, because the name is already in use in /home/web/vendor/laravel/lumen-framework/src/Application.php:661
- Stack trace:
- #0 [internal function]: Laravel\Lumen\Application->Laravel\Lumen\Concerns\{closure}(2, 'Cannot declare ...', '/Volumes/docume...', 661, Array)
- #1 /home/web/vendor/laravel/lumen-framework/src/Application.php(661): class_alias('Illuminate\\Supp...', 'Event')
- #2 /home/web/vendor/laravel/lumen-framework/src/Application.php(631): Laravel\Lumen\Application->withAliases(Array)
- #3 /home/web/vendor/laravel/lumen-framework/src/Application.php(766): Laravel\Lumen\Application->withFacades(true)
- #4 /home/web/vendor/laravel/lumen-framework/src/Console/Kernel.php(54): Laravel\Lumen\Application->prepareForConsoleCommand(true)
- #5 [internal function]: Laravel\Lumen\Console\Kernel->__construct(Object(Laravel\Lumen\Application))
- #6 /home/web/vendor/illuminate/container/Container.php(794): ReflectionClass->newInstanceArgs(Array)
- #7 /home/web/vendor/illuminate/container/Container.php(644): Illuminate\Container\Container->build('App\\Console\\Ker...', Array)
- #8 /home/web/vendor/laravel/lumen-framework/src/Application.php(211): Illuminate\Container\Container->make('App\\Console\\Ker...', Array)
- #9 /home/web/vendor/illuminate/container/Container.php(231): Laravel\Lumen\Application->make('App\\Console\\Ker...', Array)
- #10 /home/web/vendor/illuminate/container/Container.php(746): Illuminate\Container\Container->Illuminate\Container\{closure}(Object(Laravel\Lumen\Application), Array)
- #11 /home/web/vendor/illuminate/container/Container.php(644): Illuminate\Container\Container->build(Object(Closure), Array)
- #12 /home/web/vendor/laravel/lumen-framework/src/Application.php(211): Illuminate\Container\Container->make('Illuminate\\Cont...', Array)
- #13 /home/web/artisan(32): Laravel\Lumen\Application->make('Illuminate\\Cont...')
- #14 {main}
看了一下,Application.php的第661行对应的方法,我靠。
PHP代码
-
-
-
-
-
-
- public function withAliases($userAliases = [])
- {
- $defaults = [
- 'Illuminate\Support\Facades\Auth' => 'Auth',
- 'Illuminate\Support\Facades\Cache' => 'Cache',
- 'Illuminate\Support\Facades\DB' => 'DB',
- 'Illuminate\Support\Facades\Event' => 'Events',
- 'Illuminate\Support\Facades\Gate' => 'Gate',
- 'Illuminate\Support\Facades\Log' => 'Log',
- 'Illuminate\Support\Facades\Queue' => 'Queue',
- 'Illuminate\Support\Facades\Schema' => 'Schema',
- 'Illuminate\Support\Facades\URL' => 'URL',
- 'Illuminate\Support\Facades\Validator' => 'Validator',
- ];
-
- if (! static::$aliasesRegistered) {
- static::$aliasesRegistered = true;
-
- $merged = array_merge($defaults, $userAliases);
- foreach ($merged as $original => $alias) {
- class_alias($original, $alias);
- }
- }
- }
。。。。。怎么可以这样写呢?也不担心代码会不会冲突。就直接这样了??怪不得很多代码识别不了。更痛苦的是,前段时间为了测试命令行下的多线程,加载了:brew install php70-event,直接就冲突了。
不得已,brew remove php70-event。反正这个我也几乎用不到。哎~
所幸,搞定
上一篇文章我介绍的是官方的event和behavior的文章,这一篇是来自个人博客。
嗯,本来只是放在read it later做备份的,不料今天打开的时候发现,这篇文章已经不见了。该博客重新创建,原来的数据都没有了。所幸,我曾用read it later处理过纯文本模式,因此,我将它备份下来,以防哪天read it later也崩了。
ok,上原文链接。靠,原文打不开了,还是上这家网站的地址吧。。。http://www.trailroom.com/
内容其实这样的:
YII中的CComponent,CEvent与Behavior及CActiveRecordBehavior个人理解
这一块教程少,今天个人理解了下,写了个小例子,有助于理解
完成如下功能,一个JTool类,继承CComponent,当其长度改变时,调用事件,输出”change me”.
JTool.php在protected/components 下:
PHP代码
- <?php
- class JTool extends CComponent{
- private $_width;
- public function getWidth(){
- return $this->_width ? $this->_width : 1;
- }
-
- public function setWidth($width){
- if($this->hasEventHandler(‘onChange’)){
- $this->onChange(new CEvent());
- }
- $this->_width = $width;
- }
-
- public function onChange($event){
- $this->raiseEvent(‘onChange’, $event);
- }
OK,功能已经实现了,找个控制器,执行
现在我们想给JTool添加一个功能,返回长度的100倍,我们可以继承JTool.php写一个方法
[。。。TEXT后,这一段内容消失了]
OK,功能实现了,这个执行就简单了new JToolSub调用方法即可
上边的这两种办法,就是仅完成功能,下边演示Behavior及events来实现
如何用Behavior来实现上边的增加一个方法,返回长度的100倍的功能呢?
写类JBe
JBe.php在protected/behavior 下:
PHP代码
- class JBe extends CBehavior{
-
- public function get100width(){
- return $this->Owner->width*100;
- }
- }
OK,功能已经实现了,找个控制器,执行
如何用Behavior实现JTool中的长度改变时,调用一个事件的功能呢?
写类JBe:
PHP代码
- class JBe extends CBehavior{
- public function events(){
- return array_merge(parent::events(),array(
- ‘onChange’=>’change’,
- ));
- }
-
- public function change(){
- echo ‘changed’;
- }
-
- public function get100width(){
- return $this->Owner->width*100;
- }
- }
OK,功能实现随便找个控制器,执行
这里的要点是events方法
返回的数组array(‘onChange’=>’change’)定义了事件(event)和对应的事件处理方法(event hander)
事件是是Compents(JTool中)定义的,即JTool中的onChange
处理方法同由Behavior(JBe中)类定义的,即JBe中的change
这样子再看CActiveRecordBehavior,其是绑定给CActiveRecord 这个组件的,绑定方法重写behaviors()
CActiveRecordBehavior中的events() 方法返回事件及事处理函数的对应,如:
‘onBeforeSave’=>’beforeSave’
即组件CActiveRecord中的onBeforeSave这个事件对应的处理函数是
CActiveRecordBehavior中的beforeSave方法
这样子CActiveRecord在调用save()时,触发事件onBeforeSave,调用CActiveRecordBehavior对应的处理函数beforeSave
我们只要写一个CActiveRecordBehavior的子类,重写其中的beforeSave,执行一些操作,然后给CActiveRecord绑定即可
我还有个疑问,在继承CBehavior时,是不是一定要让方法events()反回那个对应关系的数组,如果这里为空,没有默认的对应关系?
---------
虽然TEXT保留了大部分内容,但还是有一小部分不见了。真可惜
本文内容来自官方的wiki,一来是做备份,二来自己查询起来也会快一点,毕竟yiiframework官网经常抽风,当然它的抽风不是因为自己,而是因为GFW,说来也可怜,好象很多技术网站都被墙了。
不过想想也正常,如果一个搞技术的,连翻墙也不会,还搞个毛技术啊。
OK,上正菜:http://www.yiiframework.com/wiki/44/behaviors-events/#hh1
These features provide endless possibilities and unbelievable flexibility, but as current documentation does not give more than a few examples, it might be difficult to fully understand their internals and requirements.
It should be noted that they do mostly the same thing. You can attach behaviors and event handlers to components to modify the components' behavior.
Events
It is useful when you want to interrupt the normal application flow without extending base classes.
For example, enabling gzip compression on the output could be done via extending CWebApplication. But because there are entry points for event handlers, one can do this:
PHP代码
- Yii::app()->onbeginRequest = create_function('$event', 'return ob_start("ob_gzhandler");'),
- Yii::app()->onendRequest = create_function('$event', 'return ob_end_flush();'),
You can create an event handler -- which is simply a method in some class with a specific signature -- and attach it to the event of an object. You can add as many event handlers as you wish, from as many objects as you wish. If the event handler is, effectively static, then you can create the object as you assign it:
PHP代码
- $test_comp->onSomethingGoesOn = array(new SomeClass, 'eventHandler1');
- $test_comp->onSomethingGoesOn = array(new SomeOtherClass, 'eventHandler2');
- $test_comp->onSomethingGoesOn = array(new YetAnotherClass, 'eventHandler3');
As long as you have a handle on the object, then you can add an event handler to it.
At some point, you can then raise the event with something like one of these:
PHP代码
- $test_comp->onSomethingGoesOn(new CEvent($this));
- $test_comp->onSomethingGoesOn(new CEvent());
So, basically, it allows you to build a list of function calls that can later be executed, in the order they were added. It can save you passing around a lot of object refs and building conditional code, since you can still raise the event, even if it doesn't do anything.
Behaviors
Behaviors are simply a way of adding methods to an object.
Take this scenario: You have 2 classes: MySuperClass1, MySuperClass2. There might be lots of methods from MySuperClass1 & 2 that you want in some new class, say MyBoringClass. Unfortunately, php does not allow for this:
PHP代码
- class MyBoringClass extends MySuperClass1, MySuperClass2 {
- }
This is where behaviors come in. Instead, you can go:
PHP代码
- class MyBoringClass extends MySuperClass1 {
- }
-
- $classInstance = new MyBoringClass();
- $classInstance->attachbehavior('uniqueName', new MySuperClass2);
Now $classInstance has all the methods from MySuperClass1 and MySuperClass2. Since MySuperClass2 is being used as a behavior, it has to extend CBehavior. The only caveat to this is an attached behavior cannot override any class methods of the component it is being attached to. If a method already exists, if it be from the original class or already added by a previously attached behavior, it will not be overwritten.
In an OO language like Ruby, it's quite possible to start with a completely empty object and simply build its behavior as you go along. Yii provides this behavior with a little magic. The key is that the class you wish to add the behavior from must extend Cbehavior.
PHP代码
- class SomeClass extends CBehavior
- {
- public function add($x, $y) { return $x + $y; }
- }
Then use with:
PHP代码
- $test_comp = new TestComponent();
- $test_comp->attachbehavior('blah', new SomeClass);
- $test_comp->add(2, 5);
So, in this case, you are extending the functionality of an object with functionality of another object.
After studying this cookbook page you are encouraged to reread the corresponding guide page as it contains advanced information (for example, if you are familiar with interfaces, you might find it enough to implement IBehavior before extending CBehavior).
-------------
behavior和Event在model里用的会相对较多一点,不过,其他地方也可以一用。如果你真的不明白这一块的逻辑,那还是用其他方式替代吧。