DEV Community

Gavin Sykes
Gavin Sykes

Posted on

Build a modern API with Slim 4 - Set up your filesystem

By now you should have a folder structure that looks a bit like the below:

- vendor
.env
.gitignore
composer.json
composer.lock
README.md - always a good idea to have one of these
Enter fullscreen mode Exit fullscreen mode

You may notice something fairly major missing at this point: where do we actually write the scripts to actually run the API? Well here is where we set that up.

There is any number of differing schools of thought on how best to set up your filesystem for simplicity and ease of development and the world really doesn't need yet another person's opinion on that. So, here's my opinion on that!

At the very minimum, you will need a public directory containing index.php, this is what all requests will be processed through when they come to your API. For now though it needn't contain anything beyond the <?php opener.

- public
  - index.php
- vendor
.env
.gitignore
composer.json
composer.lock
README.md
Enter fullscreen mode Exit fullscreen mode

In theory, this index file could contain everything: routes, controllers, modules, middleware, documentation, all of it. This can certainly be made to work, though I would not at all rate it highly on the ease-of-development scale!

Firstly I would certainly separate out the routes and controllers, in your root directory you can mkdir src/routes src/controllers in order to do that. What I end up with is below:

- aws
- config
- database
- docs
- encryption
- keys
- logging
- public
  - index.php
- src
  - controllers
  - middlewares
  - routes
  - schemas
  - scripts
  - utils
- vendor
.env
.gitignore
composer.json
composer.lock
README.md
Enter fullscreen mode Exit fullscreen mode

Some of the above will be fairly obvious as to what they will do: src is your source code (well, it all is in a sense), logging is where we'll build a logging module unless you install one with Composer. Many options are available but I have built my own so that will be part of this series. If you've guessed that aws means connecting to AWS you'd be correct, do you need to do this? Of course not, but again, I have. Next we'll get possibly the shortest bit out of the way: generating the keys to go into the keys folder, and just what they're for.

Top comments (0)