In laravel, what is the file indicated to use MacroTraits?

1

In Laravel, some classes use trait MacroableTrait .

Through this trait you can create settings like:

HTML::macro('urlQuery', function ()
{
     // Faça alguma coisa aqui
});

HTML::urlQuery(); // O método urlQuery é "criado" magicamente

However, I want to make such a definition globally.

In which Laravel file would I be able to make such settings (keeping as much as possible the organization of the code)?

bootstrap/start.php ?

app/start/global.php ?

Or another file?

    
asked by anonymous 16.07.2015 / 15:09

1 answer

1

In bootstrap/start.php I think this is a bad idea, this file is used to load autoload and start the Laravel classes itself.

No app/start/global.php would work, but you'd lose some of the organization. (Remember that app/start/global.php no longer exists in Laravel 5).

I would create a separate file inside my folder app , something like macros.php and load it by composer.json:

"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/models",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php"
    ],
    "files": [
        "app/macros.php"
    ]
},

Another way would be to define a Service Provider to create all these macros. The own documentation places an example involving the macro method in the boot method of a Service Provider in>:

Use Illuminate \ Contracts \ Routing \ ResponseFactory;

public function boot(ResponseFactory $factory)
{
    $factory->macro('caps', function ($value) {
        //
    });
}

Anyway, there is no right place for this. One of the cool points of Laravel is that it allows a flexible architecture for your application. So choose what you think is best for your project.

    
06.08.2015 / 19:06