菜单

Custom Classes in Laravel 5

2016年05月2日 - Laravel

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:


Step 2: Create an alias:

 [
     ...
        'Helper' => 'App\Helpers\Helper::class',
     ...
Step 3: Use it in your Blade template:


{!! Helper::shout('this is how to use autoloading correctly!!') !!}

Extra Credit: Use this class anywhere in your Laravel app:


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

发表评论

电子邮件地址不会被公开。 必填项已用*标注