Image not found

How to create helper function in laravel

Comment (0)

Admin

260


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!!');
    }
    ... 


Others Problem Fix Stroy



Comments (0)

Your Comment