Support the ongoing development of Laravel.io →
posted 7 years ago
IOC Forms
Last updated 4 months ago.
0

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';
	}
}
Last updated 4 months ago.
0

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()
	{
		//
	}

}
Last updated 7 years ago.
0

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?

0

@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.

0

Sign in to participate in this thread!

Full Stack Europe

Your banner here too?

orecrush orecrush Joined 12 Sep 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2022 Laravel.io - All rights reserved.