1

I want to create what I believe in Laravel is called a view helper (I'm new to Laravel so I'm not certain of the vocabulary)... in other words one or more methods that are available in all views.

Where in the directory structure would I place such a thing, and how should I use it, in a manner that's compatible with Laravel 5?

Thanks

zundi
  • 2,361
  • 1
  • 28
  • 45
  • 1
    duplicated with this question [best practice for helper functions](http://stackoverflow.com/questions/28290332/best-practices-for-custom-helpers-on-laravel-5) – Raymond Cheng Sep 29 '16 at 06:58

1 Answers1

0

I would check out the extending Blade Template example in the laravel docs

https://laravel.com/docs/5.3/blade#extending-blade

This gives you a real world example and shows you where they recommend putting it

    <?php

namespace App\Providers;

use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Perform post-registration booting of services.
     *
     * @return void
     */
    public function boot()
    {
        Blade::directive('datetime', function($expression) {
            return "<?php echo $expression->format('m/d/Y H:i'); ?>";
        });
    }

    /**
     * Register bindings in the container.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}
Brett
  • 1,951
  • 2
  • 28
  • 35