Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best location for Laravel App Utility class

Tags:

php

laravel

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?

like image 380
AndrewMcLagan Avatar asked May 03 '13 01:05

AndrewMcLagan


People also ask

Where are classes stored in Laravel?

<? php namespace App\User; class SomeClassName { ... }

What is App folder in Laravel?

The app folder is one of the major folders in Laravel as most of the code is written in the app folder. The App folder contains the following sub-folders: Console. Exceptions. Http.

Where is the default location for the views of Laravel?

Views are stored in the resources/views directory. return view('greeting', ['name' => 'James']); }); As you can see, the first argument passed to the view helper corresponds to the name of the view file in the resources/views directory.

What is Laravel root folder?

The Root Directory Structure of LaravelThe app directory holds the base code for your Laravel application. bootstrap. The bootstrap directory contains all the bootstrapping scripts used for your application. config. The config directory holds all your project configuration files (.


1 Answers

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.

like image 56
Antonio Carlos Ribeiro Avatar answered Sep 21 '22 18:09

Antonio Carlos Ribeiro