AndrewMcLagan
AndrewMcLagan

Reputation: 13987

Best location for Laravel App Utility class

I'm developing a app in laravel-4 PHP MVC framework

I'm wanting to develop some kind of utility class, for general coding tasks i carry out.

Such as: image uploading, image re-sizing etc... general application tasks

is it best practice to put all this in the base controller class? im thinking not, or defining a separate

UTILL::UtilityFunction(); 
// or 
APP:UtilityFunction();

I'm not sure of the best way to structure this and keep it within best practice?

Upvotes: 8

Views: 4886

Answers (1)

Antonio Carlos Ribeiro
Antonio Carlos Ribeiro

Reputation: 87769

You're talking about a helper class, right? You better create classes to do whatever you need them to do, but they need to have a meaning on your app, they need to be specific, there's no problem creating a small class to do some image stuff and another one really small to upload files, but one utility class that does both is not good. Take a look at those articles: http://guru-php.com/blog/2008/08/128003/ and http://blogs.msdn.com/b/nickmalik/archive/2005/09/06/461404.aspx.

Using the same logic: you should add methods to your BaseControllers that are pertinent to all your controllers.

To create your utility classes, you can create a new directory (like app/library), create your classes inside it and add this path to app/start/global.php, in ClassLoader::addDirectories() list, Laravel 4 will autoload them automatically for you. Or you can add them to composer.json, using the autoload/classmap section and then run coposer dump-autoload to autoload them.

Upvotes: 17

Related Questions