目錄
Setting the stage#
Introducing Event Annotations#
Conclusion#
首頁 php教程 php手册 Laravel 5.0 – Event Annotations

Laravel 5.0 – Event Annotations

Jun 06, 2016 pm 08:13 PM
event laravel

原文 ? 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.)

  1. Laravel 5.0 – Form Requests
  2. Laravel 5.0 – ValidatesWhenResolved
  3. Laravel 5.0 – Directory structure and namespace
  4. Laravel 5.0 – Route Caching
  5. Laravel 5.0 – Cloud File Drivers
  6. Laravel 5.0 – Method Injection
  7. Laravel 5.0 – Route Annotations
  8. Laravel 5.0 – Event Annotations
  9. Laravel 5.0 – Middleware (and how it’s replacing Filters) (coming soon)

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.

Setting the stage#

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');
登入後複製

Introducing Event Annotations#

Now, let’s drop the need for the binding entirely, and replace it with an annotation.

<?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.

Conclusion#

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.

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

在Laravel中如何獲取郵件發送失敗時的退信代碼? 在Laravel中如何獲取郵件發送失敗時的退信代碼? Apr 01, 2025 pm 02:45 PM

Laravel郵件發送失敗時的退信代碼獲取方法在使用Laravel開發應用時,經常會遇到需要發送驗證碼的情況。而在實�...

在 Laravel 中,如何處理郵件發送驗證碼失敗的情況? 在 Laravel 中,如何處理郵件發送驗證碼失敗的情況? Mar 31, 2025 pm 11:48 PM

Laravel郵件發送驗證碼失敗時的處理方法在使用Laravel...

在dcat admin中如何實現點擊添加數據的自定義表格功能? 在dcat admin中如何實現點擊添加數據的自定義表格功能? Apr 01, 2025 am 07:09 AM

在dcatadmin(laravel-admin)中如何實現自定義點擊添加數據的表格功能在使用dcat...

Laravel Redis連接共享:為何select方法會影響其他連接? Laravel Redis連接共享:為何select方法會影響其他連接? Apr 01, 2025 am 07:45 AM

Laravel框架中Redis連接的共享與select方法的影響在使用Laravel框架和Redis時,開發者可能會遇到一個問題:通過配置...

Laravel多租戶擴展stancl/tenancy:如何自定義租戶數據庫連接的主機地址? Laravel多租戶擴展stancl/tenancy:如何自定義租戶數據庫連接的主機地址? Apr 01, 2025 am 09:09 AM

在Laravel多租戶擴展包stancl/tenancy中自定義租戶數據庫連接使用Laravel多租戶擴展包stancl/tenancy構建多租戶應用時,...

Bangla 部分模型檢索中的 Laravel Eloquent ORM) Bangla 部分模型檢索中的 Laravel Eloquent ORM) Apr 08, 2025 pm 02:06 PM

LaravelEloquent模型檢索:輕鬆獲取數據庫數據EloquentORM提供了簡潔易懂的方式來操作數據庫。本文將詳細介紹各種Eloquent模型檢索技巧,助您高效地從數據庫中獲取數據。 1.獲取所有記錄使用all()方法可以獲取數據庫表中的所有記錄:useApp\Models\Post;$posts=Post::all();這將返回一個集合(Collection)。您可以使用foreach循環或其他集合方法訪問數據:foreach($postsas$post){echo$post->

在Laravel6項目中如何有效檢查Redis連接的有效性? 在Laravel6項目中如何有效檢查Redis連接的有效性? Apr 01, 2025 pm 02:00 PM

在Laravel6項目中如何檢查Redis連接的有效性是一個常見的問題,特別是在項目依賴於Redis進行業務處理時。以下是...

Laravel數據庫遷移遇到類重複定義:如何解決遷移文件重複生成及類名衝突? Laravel數據庫遷移遇到類重複定義:如何解決遷移文件重複生成及類名衝突? Apr 01, 2025 pm 12:21 PM

Laravel數據庫遷移過程中出現類重複定義問題在使用Laravel框架進行數據庫遷移時,開發者可能會遇到“類已使用�...

See all articles