FW App Structure - Server Code

The server side of each FW App handles all things data. Requests are made to it from the front-end, and data (in the form of JSON) is returned to the client. It also handles all database-related tasks, serves files when necessary, and can manage user authentication.

As introduced previously, The FW App System is intended to be modular, meaning that it is split up into several groups of related functionality. Each module will be explained in detail so that you can develop your apps with the most efficiency.

File Structure

Whenever you generate a new FW App for development, and you use either the FW Kit or FW Kit (GC) as a blueprint, there exists in the newly generated app structure a server folder, which by default has this structure:

server/
    config/
        app.php
    migrations/
        phinx.php
    src/
        App.php
        Router.php
        Model/
        Controller/
    tests/
        unit/
    index.php

This structure is in place to allow the easiest development setup for apps.

The config folder

The config folder is designed to house all of the app's config files. This includes local variables such as database connection parameters or mail server information, as well as installation-independent variables such as custom module implementation data. See the docs on the config module for more information.

  • app.php is intended to be committed to the repository, and as such no sensitive data should be stored inside.
  • local.php is designed to house local configuration for each installation. Depending on what modules you use, you can populate this with whatever data you deem necessary to house locally. The most common values stored here are database connection credentials and mail server connection credentials.

The migrations folder / Database Migrations

One key feature of FW is database migrations. These are done using Phinx, a PHP database migration library. Basically, database migrations are a way to "version" your databases, essentially committing the structure of your database into version control.

This is done using an FW Addon called migration-core, but all you need to know for now is that it automatically handles a lot of setup. You should see the migration-core documentation for more details once those docs are in existence.

FW Addons themselves will be explained in later documentation.

The src folder

The src folder contains all of the app's classes, used to define routes, controllers, models, and utilities that the server-side of your application needs.

The Controller, Model, and Router.php files will all be explained in their individual modules. The App.php file, however, is something that is good to explain here, as it needs to exist in every FW App.

In the default App.php file, there is one method, a function called onLoad(). This function is called when the server-side of your application is loaded, so use it to do any app-specific functionality that is not handled by FW Api itself. For a good example, see the one inside the Group Control App.

A Brief Note on Namespacing/Autoloading of Php classes in the src folder.

Because Php must "require" a file in order for the class to be available to your application, FW uses a library called Composer to manage the autoloading of classes/namespaces.

Composer tries to load a class based on its namespace. (You can read more about php namespaces here). It essentially converts a namespace into a file/folder hierarchy, which means that a class Foo with the namespace MyApp\Controller would map to the file my-app/server/src/Controller/Foo.php.

Unless you define a custom base namespace (more on that in later docs), your base namespace will always map to the src folder and will be based on the app name provided in the package.json file in your app's base folder.

For more insight into how Composer maps the namespace to a file, take a look at the PSR-4 standard.

The tests folder

The tests folder is where unit tests are put. Don't worry about that for now. It will be covered later in the tests section of this documentation.

index.php

The index.php file is used by the front-end to call the back-end. It should not be edited or deleted.