This answer is applicable to general custom classes within Laravel. For a more Blade-specific answer, see Custom Blade Directives in Laravel 5.
Step 1: Create your Helpers (or other custom class) file and give it a matching namespace. Write your class and method:
<?php // Code within app\Helpers\Helper.php
namespace App\Helpers;
class Helper
{
public static function shout(string $string)
{
return strtoupper($string);
}
}
Step 2: Create an alias:
<?php // Code within config/app.php
'aliases' => [
...
'Helper' => App\Helpers\Helper::class,
...
Step 3: Run composer dump-autoload
in the project root
Step 4: Use it in your Blade template:
<!-- Code within resources/views/template.blade.php -->
{!! Helper::shout('this is how to use autoloading correctly!!') !!}
Extra Credit: Use this class anywhere in your Laravel app:
<?php // Code within app/Http/Controllers/SomeController.php
namespace App\Http\Controllers;
use Helper;
class SomeController extends Controller
{
public function __construct()
{
Helper::shout('now i\'m using my helper class in a controller!!');
}
...
Source: http://www.php-fig.org/psr/psr-4/
Why it works: https://github.com/laravel/framework/blob/master/src/Illuminate/Support/ClassLoader.php
Where autoloading originates from: http://php.net/manual/en/language.oop5.autoload.php