Back

Best practices for custom helpers on Laravel 5


Hi,

I would like to create some helpers (functions) to avoid repeating code between some views. They are basically text formatting functions.

Where and how can I put a file with these functions considering that this is an application written with Laravel 5?

TorchSK replied 4 years ago

I searched for the info for some while, i did not find much so i decided to go this way.
1: I created folder app/Helpers
2: In app/Providers I created new provider file HelperServiceProvider.php
3: In this file I registered all helpers classes I need

$this->app->bind('dateHelper', function()
{
    return new \App\Helpers\DateHelper;
});
... etc

4: In config/app.php I added this new provider

'App\Providers\HelperServiceProvider',  

5: Then you need to create Facade to be availalbe to use this helper in view. You find the info about how to create Facade on offical laravel.com site

lvismer replied 4 years ago

Hi

If your helper functions are similar to the way laravel 5 ones work, i.e Illuminate/Support/helpers.php you could just create a helpers.php and have composer do the autoloading for you.

Edit your composer.json file and add the file to the autoloading key. In my example my app is called Tasky so just add the files key and reference helpers.php.

"autoload": {
    "classmap": [
        "database"
    ],
    "psr-4": {
        "Tasky\\": "app/"
    },
    "files": [
        "app/Support/helpers.php"
    ]
},

That way you just create your file inside app/Support/helpers.php and all your common function based view helpers or other helpers can go here. If you take the class based approach then the above is great.

Also then remember to just run the following once.

$ composer dumpautoload
cgrossde replied 4 years ago

I usually create a Facade which does that for me. E.g. I have a Info facade to render modals or which I call to add messages that should be shown with the next request. Then I have a CurrentUser facade which acts as a proxy to some of my user model functions. It automatically checks if the user is logged in and if not just returns nothing this way I save some if statements in my views:

@if(User::check())
   @if(User::user()->hasPermission('admin')
      <a href="admin/link">Admin</>
   @endif
@endif

{{-- Becomes --}}

@if(CurrentUser::hasPermission('admin')
   <a href="admin/link">Admin</>
@endif

{{-- Display a deletion modal which asks for confirmation to delete that record --}}
{!! Info::deleteModal($user->name, $user->name, route('admin.user.delete', $user)) !!}

The beauty of facades is that you can render views in then (e.g. my modal view) and then return the rendered view.

zot24 replied 4 years ago

Thanks!

SasSam replied 3 years ago

I think this is the perfect solution I've found so far: http://stackoverflow.com/a/28360186/3227568

vineetgarg90 replied 3 years ago

Just read this blogpost. http://www.techigniter.in/tutorials/how-to-create-helpers-file-in-laravel-5/


Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us