Where to put HTML macros in laravel 5?

2019-03-19 00:25发布

In laravel 4 I used to have HTML macros which were used in multiple views, like:

HTML::macro('minipics', function($pic)
{
    //
}

For that I had a macros.php file in the /app folder. I could not find out where to put the macros in laravel 5. Should I use the 'macroable' feature for that?

edit:

I ended up using @include(), giving it the needed values.

@include('shared.minipics', $mpics = $ppics)
@include('shared.minipics', $mpics = $randpics)

2条回答
放我归山
2楼-- · 2019-03-19 00:49

You can do it the same way as in Laravel 4.

Place your macros.php file in the /app folder.

Then in your composer.json you do this

"autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "App\\": "app/"
        },
        "files": [
            "app/macros.php"  <-- Put this here
        ]
    },
查看更多
做自己的国王
3楼-- · 2019-03-19 00:58

I've moved my macros into a "resources/macros/..." directory because I think they belong with all of the other view related code. To load them I'm using a service provider which looks something like this:

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

}
查看更多
登录 后发表回答