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?
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.
Related
I'm developing a laravel app and would like to know some best practices.
As an example, I'm thinking about creating multiple controllers instead of writing more than 10 methods in a single controller.
I would like to know what are or( if there are any ) advantages other than code readability.
My main concern is that how does it affect when there are more files to compile by the PHP compiler.
Since I'm using a framework is it going to compile all the files or only the file requested by web.php
Some insight would be great!
If the 10 methods you have in a controller are all related, then keep them in that controller. If you have a FruitController with methods related to performing actions on types of Fruit, but you also include some methods for performing actions on Vegetables, move the Vegetable methods to a new controller.
Consider encapsulation when composing your files.
In general, avoid making files for the sake of it. If it makes sense to create a new file as the logic you intend to place inside that file has no other existing home, then fine, otherwise add your logic to an existing file.
I'm not sure splitting related code into separate files increases readability, large files can be readable as long as the code is well formatted and consistent (amongst other things). Check out this book on clean code if you're interested.
What you will get though is a decrease in productivity and maintainability by having to flick through and maintain several files that are all related.
There is no advantage of using multiple controllers instead of one controller, as long as it is related to one single model. You may say it increases readability, but this is better to unify them in one single controller which is associated with your model and try to pick expressive names for the methods. The main idea is to create one single controller associated with each of your models. Feel free to add as much as methods possible into your models to talk to your database and make queries and call those methods in the associated controller. Then you can trigger those controllers through the web.php routes to handle your data and pass them to the view layer.
When you create separate controller for particular functionality this is more readable for old and new developer.
Also please check this link
From laravel:
Instead of defining all of your request handling logic as Closures in route files, you
may wish to organize this behavior using Controller classes. Controllers can group
related request handling logic into a single class. Controllers are stored in the
app/Http/Controllers directory.
I recommend that you divide your logic into different controllers. For example you can place all your user logic into one controller userController.php:
Create User
Edit User
Delete User
Then create another controller to manage the logic for another controller like sending emails etc. In this way your logic is more organized, easy to work with and you can find and update your methods easier.
SHORT VERION: Where is the common place to store the controllers for dependencies in PHP?
LONG VERSION: I need to create a project at a high quality, coming from a framework background my knowledge on directory structure is a bit rusty and I would like some help.
Current Directory Structure: https://i.imgur.com/sxowces.png
I use the "controllers" driectory for controllers as callbacks for my routes. Each HTML page I create I assign a controller to its route. I ran into a problem when I had to create a "controller" for a dependencies, for example the Twig template engine. From past experience I've learnt that you shouldn't directly call to the twig library from a created instance, you should create a class that holds an instance and make calls through that class, am I correct?
This brings a new problem, I don't know how to structure this. I'm not sure if their called controllers or not, but where do I place these in my project directory structure? What is the common practice?
Putting them inside the controllers directory seems messy, as that's reserved for page controllers. Coming from a Laravel background, I've never had to do this. So my questions are, 'Is my current directory structure okay?', and 'Where do the "controllers" for dependencies go?' ?
This seems like it should be a fairly simply question, but with Laravels (5.1) model factories, am I supposed to place every model definition inside the one ModelFactory.php file?
If, for example, I have 200 different models, I can see this file (Database\Factories\ModelFactory.php) getting very large and cluttered.
My question is:
Is there a better way to organise model factories, or should I really define them all in that one file? What is best practice?
If you have a look at the source code you can see that it takes the path (/factories) and requires every single file. So you can definitely create as many files as you want in that directory and separate them by concern or model.
I'm developing an application in Cake that heavily interacts with the filesystem. The basic idea is to monitor and index a folder and it's subfolders and files and save meta data for each file in the database. It's working fine so far, but now I got kind of a problem in understanding the MVC mechanics.
I got this heavy FilesController with a lot of functions checking if a file is up-to-date or if it has moved, updating the database entries and so on... Now I want to call some of this actions from a shell/cronjob and I want to call them in the browser too.
I've heard a lot of people complaining about importing controllers in shells and I think I got the idea why this is a bad idea. But now I want to use the same code that interacts with the FileModel directly in a shell and in a controller. Where to put it? What is best practice in this situation? In a component, loading the model? In the controller, importing it in the shell?
Thanks for your help in advance <3
I got this heavy FilesController with a lot of functions checking if a
file is up-to-date or if it has moved,
Wrong place, heavy controllers are definitely wrong. You want fat models, skinny controllers. If there is logic that extracts meta data I probably would put it into app/Utility/FileMetaData or app/Lib/FileMetaData.php and make it a new class. Depending on what it does you might extend the core class Folder or File.
The processing logic for the meta data and reading the folder should go into a model. The model can be used from the shell like in a controller by using the $uses property with an array of models.
To instanitate that class I would use a helper method (I don't mean a view helper by this!) in the model like getMetaDataReader() that returns the instance. The reason for this is to be able to mock the result of that method call in an unit test. Also, if you ever change the class or constructor args you'll have to change only a single place.
Controllers are clearly wrong in shells. There is no request to deal with. Sure technically you can do the stunt and instantiate them there but it is just wrong and doesn't make any sense. If you think you have to or "need" to do so, something is wrong with your application architecture.
You might want to take a look at my FileStorage plugin as well. You can implement an event listener there and when storing a file have the listener automatically process the the meta data.
straight to the point :
I am using Kohana, and I am looking at another script written in plain PHP. In the script, I have a class ShoppingCart. If I am to convert the script to Kohana, where am I to put the class, its methods and its properties?
Is it in my existing default controller? Or should I put it in a separate controller? Or as noobie as it may sound, will I put it in the model?
That depends on the specifics of the class I suppose. To be honest I don't know anything about Kohana, but there's probably a place for "vendor files" somewhere. Maybe it's best to place it there and write wrapper functions for it in your controller. If the class already integrates well with Kohana you may instead choose to use it as a controller or model directly. Or you might want to take the time to rewrite it to make it work as a controller...
Only you can evaluate the best place for it, there's no hard and fast rule here.
Kohana has a folder for 3rd party libraries. The main one is under system/vendor, you can put it in you application/ as well.
Many PHP class loaders require details like your filename should be the same as the class name (at least that's what I read in the Kohana documentation) if you want the classes to be automatically loaded.
If you need to use 3rd party code in your app it's recommended that you create a folder in your app / module folder called 'vendor' and place all of that code there.
You can then include the files by calling:
include kohana::find_file('vendor', 'filename');
If needs be you can also create a wrapper for the external library, a good example of this is the email helper which uses the 3rd party Swift email library.
If you're porting your own class to kohana then you need to work out what the class will be doing and categorise it accordingly.
If the class will be fetching items from some kind of database then you should make it a model. Libraries are usually sets of code that you want reuse across controllers / models, such as authentication, calendar generation etc. Controllers are used for passing data from models to your views / libraries.
See the docs for more info
As per kohana convention, you should place custom classes in application/libraries folder. However for this, you need to know how to get the class to work after putting it there. If you can't figure that out, you can do anything like putting it in your controller or making another controller of it, etc.