How to Custom Helpers in Laravel?
Create a helpers.php file in your app folder and load it up with composer:

"autoload": {
    "classmap": [
        ...
    ],
    "psr-4": {
        "App\\": "app/"
    },
    "files": [
        "app/helpers.php" // <---- ADD THIS
    ]
},

After adding that to your composer.json file, run the following command:

composer dump-autoload

If you don’t like keeping your helpers.php file in your app directory (because it’s not a PSR-4 namespaced class file), you can do what the laravel.com website does: store the helpers.php in the bootstrap directory. Remember to set it in your composer.json file:

"files": [
    "bootstrap/helpers.php"
]

Other solution: Custom Classes in Laravel, the Easy Way

This answer is applicable to general custom classes within Laravel.

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

Custom Blade Directives in Laravel

Yes, there is another way to do this!

Step 1: Register a custom Blade directive:

<?php // code in app/Providers/AppServiceProvider.php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Blade; // <-- This is important! Without it you'll get an exception.
class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
     public function boot()
     {
         // Make a custom blade directive:
         Blade::directive('shout', function ($string) {
             return trim(strtoupper($string), '(\'\')');
         });
         // And another one for good measure:
         Blade::directive('customLink', function () {
             return '<a href="#">Custom Link</a>';
         });
     }

Step 2: Use your custom Blade directive:

<!-- // code in resources/views/view.blade.php -->
@shout('this is my custom blade directive!!')
<br />
@customLink

Outputs:

THIS IS MY CUSTOM BLADE DIRECTIVE!!
Custom Link

Source: https://laravel.com/docs/5.1/blade#extending-blade

Read also >>  Easy POS and Restaurant Solution with Hold Tables

Great Article: https://mattstauffer.co/blog/custom-conditionals-with-laravels-blade-directives

LEAVE A REPLY

Please enter your comment!
Please enter your name here