Create a service provider. Thats the best place.
<?php namespace App\Providers;
use Illuminate\Html\HtmlServiceProvider;
class MacroServiceProvider extends HtmlServiceProvider
{
/**
* Register the application services.
*
* @return void
*/
public function register()
{
// Macros must be loaded after the HTMLServiceProvider's
// register method is called. Otherwise, csrf tokens
// will not be generated
parent::register();
// Load macros
require base_path() . '/path/to/your/macros.php';
}
}
In addition to the solution provided by @varghesejacob, I'm requiring my macros from the "boot" method because all the service providers have already been loaded by the time this function is run. I have also moved all my macros into a "resources/macros/*" directory because I feel like they belong with all the other view related code.
<?php namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class MacroServiceProvider extends ServiceProvider {
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
require base_path() . '/resources/macros/macro1.php';
require base_path() . '/resources/macros/macro2.php';
// etc...
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}
Why just don't add Form::macro into the routes.php
<?php
Form::macro('macros_name', function() {
... perform you fucntion here ...
});
* * *
that approach is not save? Or what?
@pavlogagin: sure it's safe, but looks messy and isn't maybe the file you think first of when you're trying to find your macro's in 6 months.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community