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

RA.
  • 969
  • 13
  • 36
  • 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? – Mathew Tinsley Oct 23 '17 at 04: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. – RA. Oct 23 '17 at 05:16
  • You could look into laravel's [Service Container](https://laravel.com/docs/5.5/container) and how to use it in [this article](https://engageinteractive.co.uk/blog/use-the-laravel-service-container) – ljubadr Oct 23 '17 at 05:22

1 Answers1

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

Mathew Tinsley
  • 6,805
  • 2
  • 27
  • 37
  • Quick note: you could create instance as `$support = app(\App\Helper\Support::class);` (without **->make()**) – ljubadr Oct 23 '17 at 05:29
  • Also, [Automatic Injection](https://laravel.com/docs/5.5/container#automatic-injection) part is worth mentioning :) – ljubadr Oct 23 '17 at 05:30
  • @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. – Mathew Tinsley Oct 23 '17 at 05:35
  • 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. – RA. Oct 23 '17 at 05:46
  • @DonaldDuck If you have a static method that depends on a constructor then it should not be static. – Mathew Tinsley Oct 23 '17 at 05:48
  • @mtinsley Can't agree with that statement. It is absolutely fine to load something in the constructor and use it in any methods within the class. Thats what `self::` is for. – RA. Oct 23 '17 at 06:30
  • Just to clarify. I'm using this approach to maintain my helper class: https://stackoverflow.com/questions/28290332/best-practices-for-custom-helpers-on-laravel-5/33776272#33776272 – RA. Oct 23 '17 at 06:49
  • @DonaldDuck Static methods should be stateless. It seems like your helper is stateful. I don't have enough background to say what you should do for sure, but my instinct is that your helper is actually a service. If you want to make static calls to your service then make a Facade. – Mathew Tinsley Oct 23 '17 at 14:30