Laravel 5.0 – Event Annotations
原文 ? 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.)
- Laravel 5.0 – Form Requests
- Laravel 5.0 – ValidatesWhenResolved
- Laravel 5.0 – Directory structure and namespace
- Laravel 5.0 – Route Caching
- Laravel 5.0 – Cloud File Drivers
- Laravel 5.0 – Method Injection
- Laravel 5.0 – Route Annotations
- Laravel 5.0 – Event Annotations
- 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.
原文地址:Laravel 5.0 – Event Annotations, 感谢原作者分享。

Alat AI Hot

Undresser.AI Undress
Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover
Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool
Gambar buka pakaian secara percuma

Clothoff.io
Penyingkiran pakaian AI

Video Face Swap
Tukar muka dalam mana-mana video dengan mudah menggunakan alat tukar muka AI percuma kami!

Artikel Panas

Alat panas

Notepad++7.3.1
Editor kod yang mudah digunakan dan percuma

SublimeText3 versi Cina
Versi Cina, sangat mudah digunakan

Hantar Studio 13.0.1
Persekitaran pembangunan bersepadu PHP yang berkuasa

Dreamweaver CS6
Alat pembangunan web visual

SublimeText3 versi Mac
Perisian penyuntingan kod peringkat Tuhan (SublimeText3)

Topik panas



Kaedah untuk mendapatkan kod kembali apabila menghantar e -mel Laravel gagal. Apabila menggunakan Laravel untuk membangunkan aplikasi, anda sering menghadapi situasi di mana anda perlu menghantar kod pengesahan. Dan pada hakikatnya ...

Tugas Jadual Laravel menjalankan penyelesaian masalah yang tidak responsif semasa menggunakan penjadualan tugas jadual Laravel, banyak pemaju akan menghadapi masalah ini: Jadual: Jalankan ...

Kaedah mengendalikan kegagalan e -mel Laravel untuk menghantar kod pengesahan adalah menggunakan Laravel ...

Cara melaksanakan fungsi jadual klik tersuai untuk menambah data dalam dcatadmin (laravel-admin) semasa menggunakan dcat ...

Kesan perkongsian sambungan Redis dalam rangka kerja Laravel dan pilih kaedah apabila menggunakan Rangka Kerja Laravel dan Redis, pemaju mungkin menghadapi masalah: melalui konfigurasi ...

Sambungan pangkalan data penyewa tersuai dalam pakej lanjutan multi-penyewa Larave Stancl/penyewaan ketika membina aplikasi multi-penyewa menggunakan pakej lanjutan multi-penyewa Larave Stancl/penyewaan, ...

Pengambilan Model Laraveleloquent: Mudah mendapatkan data pangkalan data Eloquentorm menyediakan cara ringkas dan mudah difahami untuk mengendalikan pangkalan data. Artikel ini akan memperkenalkan pelbagai teknik carian model fasih secara terperinci untuk membantu anda mendapatkan data dari pangkalan data dengan cekap. 1. Dapatkan semua rekod. Gunakan kaedah semua () untuk mendapatkan semua rekod dalam jadual pangkalan data: USEAPP \ MODELS \ POST; $ POSTS = POST :: SEMUA (); Ini akan mengembalikan koleksi. Anda boleh mengakses data menggunakan gelung foreach atau kaedah pengumpulan lain: foreach ($ postsas $ post) {echo $ post->

Kedua-dua Django dan Laravel adalah kerangka kerja penuh. Django sesuai untuk pemaju python dan logik perniagaan yang kompleks, manakala Laravel sesuai untuk pemaju PHP dan sintaks elegan. 1.Django didasarkan pada python dan mengikuti falsafah "lengkap bateri", sesuai untuk perkembangan pesat dan kesesuaian yang tinggi. 2. Laravel didasarkan pada PHP, menekankan pengalaman pemaju, dan sesuai untuk projek kecil dan sederhana.
