Fury
Fury

Reputation: 4776

Include utility class all over the application cakephp 3

I have custom utility class that contains some methods for general use and it is located in src/Utility/UtilityClass.

I can include the Utility class in controller .e.g.

use App\Utility\ArrayUtil;

and then call the class in my controllers e.g.

ArrayUtil::myMethod();

And it works. I need to include the UtilityClass in bootstrap so it applies all over the application so I can reuse it in models, templates and other controllers as well.

I tried to load it in config/bootstrap.php but I get this error:

Error: Class 'ArrayUtil' not found

Any idea

Upvotes: 1

Views: 2762

Answers (1)

Indrasis Datta
Indrasis Datta

Reputation: 8606

You can add this line at the top of the page, be it Model, view or controller.

 use App\Utility\ArrayUtil;

If you're using this utility in multiple views, then I'd suggest you to write this line in Template/Layout/default.ctp, since all templates would be a part of this.

Hope this helps.

Peace! xD

Upvotes: 3

Related Questions