1

There are many questions regarding loading custom helper classes in Laravel. However, none of them focus on the loading them with proper initialization.

As of Laravel version 5.3 we can use psr-4 autoloading which is autoloading the entire app/ directory. However, classes are loading but never initialized.

I have my helper class inside the app/helpers/support.php. This class has a constructor, where I want to load some important configuration in order to make the helper usable.

So how can I load my helper but ALSO initialize it properly in Laravel? Right now I am simply working-around the problem by using new \App\Helper\Support(); inside AppServiceProvider.php.


Edit: I'm using the following approach to maintain my helper class:

Best practices for custom helpers on Laravel 5

3
  • Can you provide some more background on what your helper does? Does it need to be initialized on every request? Are you using the instance after creating it? Commented Oct 23, 2017 at 4:59
  • I don't need it to be initialized on every page request since I'm not using this helper on every route. However, for the sake of simplicity I would like it to be ready to do the job instead of initializing it each time I need it, thats why I want it to be auto-initialized. This class is basically converting some strings depending on the site/language configuration which are loaded in the constructor. Commented Oct 23, 2017 at 5:16
  • You could look into laravel's Service Container and how to use it in this article Commented Oct 23, 2017 at 5:22

1 Answer 1

2

It seems like what you have is a service. Rather than creating an instance, you can declare it in your app service provider and inject it as a dependency when you need it.

In your register method:

$this->app->bind(\App\Helper\Support::class);

You can now use dependency injection to get an instance of your class. You can also make an instance like this:

app()->make(\App\Helper\Support::class);

If you only want one instance to exist at any given time, use singleton rather than bind.

I recommend reading the service container documentation:

https://laravel.com/docs/5.5/container

Sign up to request clarification or add additional context in comments.

8 Comments

Quick note: you could create instance as $support = app(\App\Helper\Support::class); (without ->make())
Also, Automatic Injection part is worth mentioning :)
@ljubadr that's a good point. I intentionally do not use automatic injection, I would rather have all of my services explicitly declared in my app service provider.
I'm not quite sure if this is what I need. Almost all class methods are static (obviously a helper) and this one will require me to make them dynamic(?). Instead of Support::doSomething(..) I'd have to app(\App\Helper\Support::class)->doSomething() I don't think I like that.
@DonaldDuck If you have a static method that depends on a constructor then it should not be static.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.