Home> PHP Framework> Laravel> body text

Laravel Macro

藏色散人
Release: 2020-05-19 13:39:11
forward
4120 people have browsed it

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!"
Copy after login

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
Copy after login

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'
Copy after login

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 上应用
Copy after login

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!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!