Application Structure

When we start with a new FMVC project, we can use either the CLI or compy an empty project. The resulting structure will look like this:

.
+-- config
| +-- db.json
| +-- routes.php
+-- Controller/
+-- Middleware/
+-- Models/
+-- test/
+-- Validation/
+-- views/
+-- .gitignore
+-- composer.json
+-- index.php
+-- README.md

This may seem a little much at first glance but we will walk through the structure step by step and look on the whats and whys of it.

config

The first directory in our list is called config in this folder all configuration should reside. Right now there are only two files in here:

  • db.json
  • routes.php

db.json ๐Ÿ—‚

db.json stores all information about our database connections. Right after creation it looks roughly like this:

{
"default": {
"protocol": "sqlite",
"host": "file.db",
"username": "",
"password": "",
"port": "",
"database": ""
},
"test": {
"protocol": "sqlite",
"host": "test.db",
"username": "",
"password": "",
"port": "",
"database": ""
}
}

It holds two default connections used to provide a data connection for development (default) and for testing (test). For a detailed explanation take a look on

Databases
.

routes.php ๐Ÿ—บ

A bit unconventional is the other file in the config directory: routes.php.

When developing applications with FMVC, every request will be redirected to index.php. Here the router will check routes.php for the way it should handle requests.

Here's an example

<?php
return [
'base_url' => '/', #please do only add subfolders
'authenticate' => [
'GET' => [
"controller" => 'Session::create',
"middleware" => ['Middleware::authorize'],
"validation" => []
]
],
'article/create' => [
'POST' => [
"controller" => 'Article::create',
'validation' => 'ArticleModel'
]
],
];

The routes file exports an associative array where the keys represent the route, that is being handled, and the value represents an array containing the handling configuration:

[
# index url
'' => [
# configuration for HTTP post call
'POST' => [
# controller class with static handler method
'controller' => 'Controller::handler',
# array with middelware methods
'middleware' => [],
# name of the validation shema for the request data
'validation' => 'ValidationClass'
]
]
]

Controller

This folder holds the application controllers. A controller is an encapsulated entity, providing methods to handle requests.

More information about controllers and how to use them can be found here

Middleware

In the Middleware directory resides all middleware available to the application. When middleware is configured for a route, the framework will take a quick look into the directory and load the corresponding class. An introduction to middleware and how FMVC uses it, refer to Middleware.

test

test - as the name already spoils - contains the unit tests for your application. Right now the components for making testing easier are still in development ๐Ÿ‘ท๐Ÿ—.

But when everything's finished, the guide will reside here!

Validation

Validation of a data structure is a big issue in every application. Fortunately, FMVC can draw on DRY::PHP. DRY::PHP is a validation library for objects as well as arrays. When the validation key is set in a route configuration, FMVC will search in this directory for the specified class.

More on validation and how it is being used here.

views

Sometimes we want to render complex HTML views and send them as a response to the client - in the past this has proven as a solid practice when creating application for web browsers. So when you want to get really freaky, this folder will hold all your templates!๐Ÿ˜

More info on that in Views