Home>Article>PHP Framework> Laravel Macro

Laravel Macro

藏色散人
藏色散人 forward
2020-05-19 13:39:11 4121browse

The following is developed byLaravelThe introductory tutorial column will introduce you to the magical Laravel macro instruction (Macro). I hope it will be helpful to friends in need!

Laravel Macro

Have you ever wanted a feature in Laravel, but it didn’t really exist? Let me introduce you to Laravel macros. Macros allow you to add custom functionality to Laravel's internal components.

Let us take a simple Request facade method as an example.

Request::macro('introduce', function ($name) { echo 'Hello ' . $name . '!'; }); Request::introduce('Caleb'); // outputs "Hello Caleb!"

A more practical Request macro is used to detect the current TLD (top-level domain: .com, .net, .org, .etc...).

Request::macro('tldIs', function ($tld) { return Str::is('*.' . $tld, $this->root()); }); Request::tldIs('com') // returns true for app.com Request::tldIs('dev') // returns false for app.com

You will notice that Laravel automatically binds $this to the context of the Request, rather than in a class that has defined the macro. For example:

class AppServiceProvider { public function boot() { Request::macro('context', function () { return get_class($this); } } ... Request::context(); // returns 'Illuminate\Http\Request' // instead of 'App\AppServiceProvider'

Let’s look at a more advanced example. This macro conditionally adds a where statement on the model based on the current TLD.

Builder::macro('whenTldMatches', function($tld, $callback) { if (Request::tldIs($tld)) { call_user_func($callback->bindTo($this)); } return $this; }); SomeModel::whenTldMatches('org', function () { $this->where('id', '>', 5); })->get(); // applies ->where() 在 app.org 上应用,而不在 app.com 上应用

Where should we define them?

Service providers are a great place to define macros for your application.App\Providers\AppServiceProvider boot()isIa good injection point, but it gets bloated quickly.

The next step is to create aApp\Providers\MacrosServiceProviderand register it inconfig/app.php. If a macro is relevant, I might create anApp\Providers\TldAwareServiceProviderto hold allTLDrelated macros.

Which components are Macroable?

Macros can be defined on any class with the Macroable attribute. The following is a list of Macroable's facades and classes

Facade

● Cache

● File

● Lang

● Request

● Response

● Route

● URL

Illuminate Classes

● Illuminate\Cache\Repository

● Illuminate\Console\Scheduling\Event

● Illuminate\Database\Eloquent\Builder

● Illuminate\Database\Eloquent\Relation

● Illuminate\Database\Query\Builder

● Illuminate\Filesystem\Filesystem

● Illuminate\Foundation\Testing\TestResponse

● Illuminate\Http\RedirectResponse

● Illuminate\Http\Request

● Illuminate\Http\UploadedFile

● Illuminate\Routing\ResponseFactory

● Illuminate\Routing\Router

● Illuminate\Routing\UrlGenerator

● Illuminate\Support\Arr

● Illuminate\Support\Collection

● Illuminate\Support\Str

● Illuminate\Translation\Translator

● Illuminate\Validation\Rule

Hands-On

If you find yourself duplicating Laravel components throughout your system To perform logic, consider using macros for better expression and reuse. Believe me, very greedy.

Good luck!

For more laravel framework technical articles, please visitlaraveltutorial!

The above is the detailed content of Laravel Macro. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete