What Are Macros?

  • A way to extend a class without inheritance
  • Defined as a simple closure with as many arguments as you need
  • Decouples additional functionality from the framework
  • Available on any Laravel class that uses the Macroable trait
<?php

namespace App\Providers;

use Illuminate\Support\Facades\Response;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Response::macro('caps', function ($value) {
            return Response::make(strtoupper($value));
        });
    }
}

← / → navigate · N notes · Esc exit