原文 ? http://mattstauffer.co/blog/laravel-5.0-event-annotations Posted on October 10, 2014 | By Matt Stauffer (this is part of a series of posts on new features in laravel 5.0. check back soon for more.) Laravel 5.0 Form Requests Laravel
Posted on October 10, 2014 | By Matt Stauffer
(this is part of a series of posts on new features in laravel 5.0. check back soon for more.)
In 5.0, Laravel is moving more and more of the top-level, bootstrapped, procedural bindings and definitions into a more Object-Oriented, separation-of-concerns-minded structure. Filters are now objects, controllers are now namespaced, the PSR-4-loaded application logic is now separate from the framework configuration, and more.
We saw in thelast postthat annotations are one of the ways Laravel 5.0 is making this change. Where routes used to be bound one after another in routes.php, they now can be bound with annotations on the controller class and method definitions.
Another part of Laravel that has traditionally been bound with a list of calls one after another is event listeners, and this is the next target of the annotation syntax.
Consider the following code:
Event::listen('user.signup', function($user)
{
$intercom = App::make('intercom');
$intercom->addUser($user);
});Somewhere in your code—in a service provider, maybe, or maybe just in a global file somewhere—you’ve bound a listener (the closure above) to the “user.signup” event.
Of course, you’re probably noticing that all that closure does is call a single method—so we could refactor it to this:
Event::listen('user.signup', 'Intercom@addUser');Now, let’s drop the need for the binding entirely, and replace it with an annotation.
BeikeShop 一款开源好用的跨境电商系统,BeikeShop 是基于 Laravel 开发的一款开源商城系统主要面向外贸/跨境电商行业提供商品管理、订单管理、会员管理、支付、物流、系统管理等功能。BeikeShop系统亮点1、系统代码100%开源 2、代码分层清晰、格式规范 3、基于Laravel框架开发 4、Event机制实现Hook功能 5、强大的插件机制 6、系统扩展性强 方便二次开发
0
<?php namespace App;
class Intercom
{
/**
* @Hears("user.signup")
*/
public function addUser(User $user)
{
return $this->api_wrapper->sendSomeAddThing(
$user->email,
$user->name
);
}
}As you can see, the @Hears annotation can take a string event name, but it can also take an array of event names (in annotations, arrays are surrounded by {} instead of []). Now, run artisan event:scan and you’ll get a file namedstorage/framework/events.scanned.php , with the following contents:
<?php $events->listen(array ( 0 => 'user.signup', ), 'App\Intercom@addUser');
Instantly bound.
There are positives and negatives to working with your event system this way.
The primary negative I see is that you could look at this annotation as being framework-specific; if that’s the case, you’re now placing framework-specific code directly into your domain. If you imagine this Intercom class being something you’re passing around between several sites, its binding may be specific to this site–in which case you’d be better off using the classic style of binding. However, that’s not always the case.
Note that this negative is different from the same situation in Route Annotations, which are only being applied to Controllers–which are not domain objects.
The positives I can see at first glance are that first, you’re defining the method’s act of listening on the method itself, rather than elsewhere; and second, that you’re defining the listener in a way that it can be programmatically accessed (meaning you could, at any point, replace artisan event:scan with a program of your own devising that outputs something other than a Laravel events.scanned file). There are likely smarter folks than me that’ll weigh in on this.
原文地址:Laravel 5.0 – Event Annotations, 感谢原作者分享。
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
C++高性能并发应用_C++如何开发性能关键应用
Java AI集成Deep Java Library_Java怎么集成AI模型部署
Golang后端API开发_Golang如何高效开发后端和API
Python异步并发改进_Python异步编程有哪些新改进
C++系统编程内存管理_C++系统编程怎么与Rust竞争内存安全
Java GraalVM原生镜像构建_Java怎么用GraalVM构建高效原生镜像
Python FastAPI异步API开发_Python怎么用FastAPI构建异步API
C++现代C++20/23/26特性_现代C++有哪些新标准特性如modules和coroutines
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号