Inhaltsverzeichnis
Setting the stage#
Introducing Event Annotations#
Conclusion#
Heim 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);
});
Nach dem Login kopieren

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');
Nach dem Login kopieren

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
    );
  }
}
Nach dem Login kopieren

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');
Nach dem Login kopieren

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.

Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

Video Face Swap

Video Face Swap

Tauschen Sie Gesichter in jedem Video mühelos mit unserem völlig kostenlosen KI-Gesichtstausch-Tool aus!

Heiße Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Wie bekomme ich den Rückgabecode, wenn das Senden von E -Mails in Laravel fehlschlägt? Wie bekomme ich den Rückgabecode, wenn das Senden von E -Mails in Laravel fehlschlägt? Apr 01, 2025 pm 02:45 PM

Methode zum Abholen des Rücksendecode, wenn das Senden von Laravel -E -Mails fehlschlägt. Wenn Sie Laravel zur Entwicklung von Anwendungen verwenden, stellen Sie häufig Situationen auf, in denen Sie Überprüfungscodes senden müssen. Und in Wirklichkeit ...

Laravel -Zeitplanaufgabe wird nicht ausgeführt: Was soll ich tun, wenn die Aufgabe nicht nach Zeitplan ausgeführt wird: Befehl ausführen? Laravel -Zeitplanaufgabe wird nicht ausgeführt: Was soll ich tun, wenn die Aufgabe nicht nach Zeitplan ausgeführt wird: Befehl ausführen? Mar 31, 2025 pm 11:24 PM

Laravel -Zeitplan -Aufgabe Ausführen nicht reagierende Fehlerbehebung Bei Verwendung der Zeitplanung von Laravel -Zeitplänen werden viele Entwickler auf dieses Problem stoßen: Zeitplan: Run ...

Wie kann man in Laravel mit der Situation umgehen, in der Überprüfungscodes nicht per E -Mail gesendet werden? Wie kann man in Laravel mit der Situation umgehen, in der Überprüfungscodes nicht per E -Mail gesendet werden? Mar 31, 2025 pm 11:48 PM

Die Methode zum Umgang mit Laravels E -Mail -Versagen zum Senden von Verifizierungscode besteht darin, Laravel zu verwenden ...

So implementieren Sie die benutzerdefinierte Tabellenfunktion des Klickens, um Daten im DCAT -Administrator hinzuzufügen? So implementieren Sie die benutzerdefinierte Tabellenfunktion des Klickens, um Daten im DCAT -Administrator hinzuzufügen? Apr 01, 2025 am 07:09 AM

So implementieren Sie die Tabellenfunktion von benutzerdefiniertem Klicken, um Daten in dcatadmin (laravel-admin) hinzuzufügen, wenn Sie DCAT verwenden ...

Laravel Redis -Verbindungsfreigabe: Warum wirkt sich die Auswahlmethode auf andere Verbindungen aus? Laravel Redis -Verbindungsfreigabe: Warum wirkt sich die Auswahlmethode auf andere Verbindungen aus? Apr 01, 2025 am 07:45 AM

Die Auswirkungen des Austauschs von Redis -Verbindungen im Laravel -Framework und der Auswahl von Methoden bei Verwendung von Laravel -Framework und Redis können Entwickler auf ein Problem stoßen: Durch Konfiguration ...

Laravel Multi-Tenant-Erweiterungsstanz/Mietverhältnis: Wie passen Sie die Host-Adresse einer Mieterdatenbankverbindung an? Laravel Multi-Tenant-Erweiterungsstanz/Mietverhältnis: Wie passen Sie die Host-Adresse einer Mieterdatenbankverbindung an? Apr 01, 2025 am 09:09 AM

Benutzerdefinierte Mieterdatenbankverbindung in Laravel Multi-Tenant-Erweiterungspaket Stanz/Mietverhältnis beim Erstellen von Multi-Mandanten-Anwendungen mit Laravel Multi-Tenant-Erweiterungspaket Stanz/Mietverhältnis, ...

Laravel eloquent orm bei bangla partieller Modellsuche) Laravel eloquent orm bei bangla partieller Modellsuche) Apr 08, 2025 pm 02:06 PM

Laraveleloquent-Modellab Abruf: Das Erhalten von Datenbankdaten Eloquentorm bietet eine prägnante und leicht verständliche Möglichkeit, die Datenbank zu bedienen. In diesem Artikel werden verschiedene eloquente Modellsuchtechniken im Detail eingeführt, um Daten aus der Datenbank effizient zu erhalten. 1. Holen Sie sich alle Aufzeichnungen. Verwenden Sie die Methode All (), um alle Datensätze in der Datenbanktabelle zu erhalten: UseApp \ Models \ post; $ posts = post :: all (); Dies wird eine Sammlung zurückgeben. Sie können mit der Foreach-Schleife oder anderen Sammelmethoden auf Daten zugreifen: foreach ($ postas $ post) {echo $ post->

Was ist besser, Django oder Laravel? Was ist besser, Django oder Laravel? Mar 28, 2025 am 10:41 AM

Sowohl Django als auch Laravel sind Full-Stack-Frameworks. Django eignet sich für Python -Entwickler und komplexe Geschäftslogik, während Laravel für PHP -Entwickler und elegante Syntax geeignet ist. 1.Django basiert auf Python und folgt der "batteriebetriebenen" Philosophie, die für schnelle Entwicklung und hohe Parallelität geeignet ist. 2. Laravel basiert auf PHP, der die Entwicklererfahrung betont und für kleine bis mittlere Projekte geeignet ist.

See all articles