What are Laravel Macros and How to Extending Laravel’s Core Classes using Macros with example?
https://ift.tt/2MjbrVN
Laravel Macros are a great way of expanding Laravel’s core macroable classes and add additional functionality needed for your application. In simple word, Laravel Macro is an approach to add some missing functionality to Laravel’s core component with a piece of code which doesn’t exist in the Laravel class. To implement a Laravel Macro, Laravel gives a PHP trait called Macroable. You can check Illuminate\Http\Response class of Laravel, which implements the Macroable trait, which implies you can extend the Illuminate\Http\Response class using a macro static method.
Registering a new macro
All macros should be registered inside the boot method in the service provider. You can create a dedicated service provider for macros, or you can add macros in the AppServiceProvider, shipped with the default Laravel installation. That is totally up to you.
Using anonymous function
This approach is a straightforward approach to add a new macro is putting it just inside the boot method for AppServiceProvider.
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
Collection::macro('name it here', function(){
// Pass your actual code here.
});
}
}
And you are done.
Here is an example of toUpper macro:
use Illuminate\Support\Str;
Collection::macro('toUpper', function () {
return $this->map(function ($value) {
return Str::upper($value);
});
});
$collection = collect(['first', 'second']);
$upper = $collection->toUpper();
// ['FIRST', 'SECOND']
Using mixins
If you want to use lots of macros from a particular Laravel class. For this situation, your boot method of service provider most likely gets bigger with various macros, and the code begins looking messy.
To isolate your code, you can utilize mixins.
For this approach, you should utilize a mixin static method on the macroable class, and pass your mixin class as an argument.
Let’s take a look at an example. Let’s say, we have a Macros/QueryBuilderMacros.php file with the following content.
class QueryBuilderMacros
{
public function one()
{
// macro content
}
protected function two()
{
// macro content
}
private function three()
{
// will not become a macro
}
}
The following code is the example of mixin to register QueryBuilderMacros class with its methods.
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
Builder::mixin(new QueryBuilderMacros());
}
}
That is it. Methods of QueryBuilderMacros class, one and two will become available on each Builder class instance. Method three declared as private, will remain accessible just for methods in QueryBuilderMacros class, you know since private should remain private.
Conclusion
Laravel’s macros are a compelling component, which opens a lot of conceivable outcomes to expand the Laravel framework, and helps to get rid of repeating the same logic across the application.
programming
via Laravel News Links https://ift.tt/2dvygAJ
January 29, 2021 at 03:06PM