Using custom helpers without a full path - Laravel 5.1

I have a method that truncates a string.

StringHelper::truncate() 

When I use this method, in my opinion, it cannot find the StringHelper class. I added it to composer.json and autostored the dump.

It works when I specify the full path:

 \App\Http\Helpers\StringHelper 

But I would like to have the first option.

How can i do this? Perhaps AppServiceProvider, but how?

+4
source share
2 answers

The right way for Laravel 5.1

  • Create the Blade directive in the AppServiceProvider class:
 class AppServiceProvider extends ServiceProvider { public function boot() { Blade::directive('truncate', function ($expression) { return "<?php echo trim({$expression}); ?>"; }); } } 
  1. Use it like this:
 @truncate($string) 
+4
source

Answer for Laravel 5

Add the auxiliary file to the "files" part of your composer.json (at the root of your application), and you will not need to specify the full path every time:

 "files": [ "App/Http/Helpers/StringHelper.php" ], 
0
source

All Articles