This is a very general question but since the Slim community seems to have a high standard for code organization and has given me some of the best coding advice around I’d figure this was worth asking.
As my application is growing I’m a little unclear about what is the role of some Classes in my application and how I should organize things. For example my application directory is set up like this:
src/ middleware/ Auth.php dependencies.php settings.php routes.php
My Auth.php class is obviously middleware because it needs to be executed during each route (checking for logged in status, etc…
Is every class I create “middleware” and therefor belongs in the middleware directory? In the past I thought of every Class that worked with the database as a “Model” are they all just considered “middleware” now?
How does one call the complex logic that may occur within a class?
Would you call individual methods from the route?
Should all of my classes be located in the container?
(I know there’s many ways to do this but, I respect the design principles that most members here have)
Let’s look at an example… Say I have a route that needs to:
- Handle an image upload
- Verify the image
- Resize the image and save
- Resize the image and save again
Would it be wise to call each method from inside the route:
$app->post('/image', function ($request, $response, $args) {
$img = new Img();
if($img->upload()){
if($img->verify(){
if($img->resize(400)){
if($img->resize(200)){
}
}
}
}
});
or should I let the class handle the logic and method calling:
$app->post('/image', function ($request, $response, $args) { $img = new Img(); $img->do_all_img_stuff(); });
Thanks!