DEV Community

Gavin Sykes
Gavin Sykes

Posted on

1

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.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay