手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表分类:PHP

Lumen启动时出现:NotFoundHttpException

Lumen在初次安装好启动的时候,访问会出现:NotFoundHttpException

找了一下原因,在stackoverflow上有很多人提出来。常见的有这两种:在public/index.php里修改最后的$app->run();

  1. $app->run($app->make('request'));
  2. $app->run(Illuminate\Http\Request::capture());
如果用第1种,那你会发现所有的路由都被"/"解析了。你随便在URL里输入啥,都只会进入$app->get("/")这个路由
只有第二种才是正确的。
 
记录一下,参考 :
1、http://stackoverflow.com/questions/29728973/notfoundhttpexception-with-lumen
2、http://stackoverflow.com/questions/36436967/just-installed-lumen-and-got-notfoundhttpexception
 

Tags: lumen

laravel mass update can not fire any events

 一个很简单的laravel代码:

PHP代码
  1. User::where('id',1)->update(['last_login'=>time()]);  
这样一个简单的代码,你会发现,它并没有触发:updating,updated事件,然而,你又会发现,updated_at也更新了。这是怎么回事?
跟进代码看一下,你会发现这个update,是Eloquent\Builder的方法,它的update方法代码很简单:
PHP代码
  1. /** 
  2.  * Update a record in the database. 
  3.  * 
  4.  * @param  array  $values 
  5.  * @return int 
  6.  */  
  7. public function update(array $values)  
  8. {  
  9.     return $this->toBase()->update($this->addUpdatedAtColumn($values));  
  10. }  
我擦 ,要不要这么直接?再看一下toBase,这回是调用的是Query\Builder了。这里的update,就纯粹是生成sql,然后execute,不再走EloquentORM的事件了,所以就不会触发任何事件。
 
那么要怎么样才能触发事件呢?方法有两种
1、不要这样简写:先读数据再update,就一定会触发
2、不要用where(),而是直接User::find(1)->update([]),这时候就会从ORM走了。
 
这事儿官方有解释:
https://laravel.com/docs/5.3/eloquent#updates
  1. When issuing a mass update via Eloquent, the saved and updated model events will not be fired for the updated models. This is because the models are never actually retrieved when issuing a mass update.  
同时,这事儿在stackoverflow上也有很多人在问,有人回复的就是和我上面的一样:
http://stackoverflow.com/questions/41295032/laravel-eloquent-model-update-event-is-not-fired
  1. You need to retrieve the user from the database and then save that user in order to fire the event. For example:  
  2.   
  3. This will NOT fire the update event:  
  4.   
  5. User::where('id', $id)->update(['username' => $newUsername]);  
  6. This will fire the update event:  
  7.   
  8. User::find($id)->update(['username' => $newUsername]);  
官方的解释我是看过了,下面的是我用了很多办法没法解决后才找到的。
我在想,use SoftDeletes可以注入代码,为什么我不行?直到我发现softdeletes是官方自带库,人家都准备了事件:onDelete了。。怎么就没有onUpdate事件可以让我加点字段呢?
然后我又想用其他的Scope,也没有成功(折腾了一个下午)
 
暂时放弃!就这样吧,不过我还在想想,有没有什么办法可以注入,因为在sql生成完后,它还是有触发的事件的,比如:
PHP代码
  1. /** 
  2.   * Log a query in the connection's query log. 
  3.   * 
  4.   * @param  string  $query 
  5.   * @param  array   $bindings 
  6.   * @param  float|null  $time 
  7.   * @return void 
  8.   */  
  9.  public function logQuery($query$bindings$time = null)  
  10.  {  
  11.      if (isset($this->events)) {  
  12.          $this->events->fire(new Events\QueryExecuted(  
  13.              $query$bindings$time$this  
  14.          ));  
  15.      }  
  16.   
  17.      if ($this->loggingQueries) {  
  18.          $this->queryLog[] = compact('query', 'bindings', 'time'); 
  19.      } 
  20.  } 
  21.  
  22.  /** 
  23.   * Register a database query listener with the connection. 
  24.   * 
  25.   * @param  \Closure  $callback 
  26.   * @return void 
  27.   */ 
  28.  public function listen(Closure $callback) 
  29.  { 
  30.      if (isset($this->events)) { 
  31.          $this->events->listen(Events\QueryExecuted::class, $callback); 
  32.      } 
  33.  } 
  34.  
  35.  /** 
  36.   * Fire an event for this connection. 
  37.   * 
  38.   * @param  string  $event 
  39.   * @return void 
  40.   */ 
  41.  protected function fireConnectionEvent($event) 
  42.  { 
  43.      if (! isset($this->events)) { 
  44.          return; 
  45.      } 
  46.  
  47.      switch ($event) { 
  48.          case 'beganTransaction': 
  49.              return $this->events->fire(new Events\TransactionBeginning($this)); 
  50.          case 'committed': 
  51.              return $this->events->fire(new Events\TransactionCommitted($this)); 
  52.          case 'rollingBack':  
  53.              return $this->events->fire(new Events\TransactionRolledBack($this));  
  54.      }  
  55.  }  
实在不行了,还可以在事务上面想想办法,但是总归是只能一次处理,如果是需要两次执行sql。那还不如直接写代码呢。等有空再看了
 
 
 

sablog for PHP7

 想来这东西应该没多少人用了。不过我还是上传一份吧。

sablog.zip
就是对着BUG改的,应该也没有什么太特别的玩意。有需要的就下载

Tags: sablog

还好没有手贱升级PHP7

本来想将当前的服务器升级到PHP7的,在临最后动手的一霎那,突然想起,sablog用的是mysql_*的函数,在PHP7已经彻底没有了。

感慨一下没有手贱啊。不然就死啦死啦的了。准备这两天将原来的mysql类改用PDO实现一下,然后本地跑一下,看看PHP7是不是可以运行。如果OK的话,准备升级!

Mac PHP7 Lumen Event冲突

在使用Lumen进行开发的时候。如果你使用:./artisan的时候报错:Cannot declare class Event, because the name is already in use。检查一下LOG发现是:

XML/HTML代码
  1. [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  
  2. Stack trace:  
  3. #0 [internal function]: Laravel\Lumen\Application->Laravel\Lumen\Concerns\{closure}(2, 'Cannot declare ...', '/Volumes/docume...', 661, Array)  
  4. #1 /home/web/vendor/laravel/lumen-framework/src/Application.php(661): class_alias('Illuminate\\Supp...', 'Event')  
  5. #2 /home/web/vendor/laravel/lumen-framework/src/Application.php(631): Laravel\Lumen\Application->withAliases(Array)  
  6. #3 /home/web/vendor/laravel/lumen-framework/src/Application.php(766): Laravel\Lumen\Application->withFacades(true)  
  7. #4 /home/web/vendor/laravel/lumen-framework/src/Console/Kernel.php(54): Laravel\Lumen\Application->prepareForConsoleCommand(true)  
  8. #5 [internal function]: Laravel\Lumen\Console\Kernel->__construct(Object(Laravel\Lumen\Application))  
  9. #6 /home/web/vendor/illuminate/container/Container.php(794): ReflectionClass->newInstanceArgs(Array)  
  10. #7 /home/web/vendor/illuminate/container/Container.php(644): Illuminate\Container\Container->build('App\\Console\\Ker...', Array)  
  11. #8 /home/web/vendor/laravel/lumen-framework/src/Application.php(211): Illuminate\Container\Container->make('App\\Console\\Ker...', Array)  
  12. #9 /home/web/vendor/illuminate/container/Container.php(231): Laravel\Lumen\Application->make('App\\Console\\Ker...', Array)  
  13. #10 /home/web/vendor/illuminate/container/Container.php(746): Illuminate\Container\Container->Illuminate\Container\{closure}(Object(Laravel\Lumen\Application), Array)  
  14. #11 /home/web/vendor/illuminate/container/Container.php(644): Illuminate\Container\Container->build(Object(Closure), Array)  
  15. #12 /home/web/vendor/laravel/lumen-framework/src/Application.php(211): Illuminate\Container\Container->make('Illuminate\\Cont...', Array)  
  16. #13 /home/web/artisan(32): Laravel\Lumen\Application->make('Illuminate\\Cont...')  
  17. #14 {main}  

看了一下,Application.php的第661行对应的方法,我靠。

PHP代码
  1. /** 
  2.      * Register the aliases for the application. 
  3.      * 
  4.      * @param  array  $userAliases 
  5.      * @return void 
  6.      */  
  7.     public function withAliases($userAliases = [])  
  8.     {  
  9.         $defaults = [  
  10.             'Illuminate\Support\Facades\Auth' => 'Auth',  
  11.             'Illuminate\Support\Facades\Cache' => 'Cache',  
  12.             'Illuminate\Support\Facades\DB' => 'DB',  
  13.             'Illuminate\Support\Facades\Event' => 'Events',  
  14.             'Illuminate\Support\Facades\Gate' => 'Gate',  
  15.             'Illuminate\Support\Facades\Log' => 'Log',  
  16.             'Illuminate\Support\Facades\Queue' => 'Queue',  
  17.             'Illuminate\Support\Facades\Schema' => 'Schema',  
  18.             'Illuminate\Support\Facades\URL' => 'URL',  
  19.             'Illuminate\Support\Facades\Validator' => 'Validator',  
  20.         ];  
  21.   
  22.         if (! static::$aliasesRegistered) {  
  23.             static::$aliasesRegistered = true;  
  24.   
  25.             $merged = array_merge($defaults$userAliases);  
  26.             foreach ($merged as $original => $alias) {  
  27.                 class_alias($original$alias);  
  28.             }  
  29.         }  
  30.     }  

。。。。。怎么可以这样写呢?也不担心代码会不会冲突。就直接这样了??怪不得很多代码识别不了。更痛苦的是,前段时间为了测试命令行下的多线程,加载了:brew install php70-event,直接就冲突了。

不得已,brew remove php70-event。反正这个我也几乎用不到。哎~

所幸,搞定

 

 

 

Tags: lumen, event