Support the ongoing development of Laravel.io →
Configuration
Last updated 7 months ago.
0

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

Last updated 7 years ago.
0

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
Last updated 7 years ago.
0

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.

Last updated 7 years ago.
0

Thanks!

0

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

Last updated 7 years ago.
0

Sign in to participate in this thread!

LaraJobs

Your banner here too?

Moderators

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

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2022 Laravel.io - All rights reserved.