Laravel

Custom Blade Directives in Laravel 5

Step 1: Register a custom Blade directive:

Custom Link';
         });
     }
    ...
Step 2: Use your custom Blade directive:



@shout('this is my custom blade directive!!')

@customLink

Outputs:

THIS IS MY CUSTOM BLADE DIRECTIVE!!
Custom Link


Custom Classes in Laravel 5

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


Copyright © 1996-2010 Add Lives. All rights reserved.
iDream theme by Templates Next | Powered by WordPress