Models, middleware & philosophy / architecture

I’ll take a stab at some of these questions. You may want to have a look at PHP-PDS/skeleton, although most of your questions are about what goes into the src directory. In my case I call it app, but that’s just a preference.

In my app (or src) directory, I keep directories for Controllers, Middleware, Models, Providers, Traits, and Views. But again, that is just my preference. In your example, when hitting that route I’d call a Controller, perhaps called ImageUploadController, and call a create() method on the controller. Traits and Views are probably obvious in their use, and I use Providers for classes that hook into external services like other APIs, sending email, etc. Those then get used by my controllers where appropriate.

Have a look at the amazing @akrabat’s Slim Bookshelf example app too.

$app->post('/image', 'Controllers\ImageUploadController:create')->add('App\Middleware\AuthMiddleware');

The create method on the controller might then take the image and call various methods on some sort of an Image model (perhaps stored at /src/Models/Image.php) to validate() then resize(1024, 766), then save(). Finally the controller might look for a View stored at /views/image/upload.html.twig, render the view, and return the response.

Your logic above starting at $img = would therefore partially live within the controller. The controller methods should be fairly simple and call methods on your model, more like the two $img lines within your last code example. Maybe something like this.

public function create(Request $request, Response, $response, $image) {
    $img = new Image();

    if (! $img->upload()) {
        // return view with error
    }

    if (!$img->verify() {
        // return view with different error
    }

    $img->resize(200);
    $img->save();

    return $this->view->render($response, 'views/image/upload-success.html.twig');
}

Of course don’t expect any of that code to work. :slight_smile:

While based on Laravel, you might find this video series well worth watching and I believe it is free. I subscribe to Laracasts even though I don’t use Laravel (though I do use Eloquent). The tutorials are amazing. Since you’re on your own to provide Slim with what Laravel provides for itself, you can apply many of the principles Laravel uses to build up your own Slim app.