Controllers

Controllers are entry points into your application that make sense for humans beings in modern web applications. The way of routing request to a Controller is usually by analyzing the application URL for each request.

The main use case of Controllers in Strata is to replace the need of placing pure code in template file. Instead of instantiating queries and various variables inside a template file, you should place this code in a Controller. The biggest and most obvious gain is the ability to use the same business logic code multiple times as well as improving code testability and readability.

Once you have set up at least one application route in your project’s configuration file you must build the corresponding controller endpoints to complete the process.

Creating a controller file

To generate a Controller, you should use the automated generator provided by Strata. It will validate your object’s name and ensure it is defined following the intended conventions.

Using the command line, run the generate command from your project’s base directory. In this example, we will generate a controller for the Artist object:

$ ./strata generate controller artist

The command generates a couple of files for you, including the actual Controller file and test suites for the generated class.

Scaffolding controller ArtistController
  ├── [ OK ] src/Controller/ArtistController.php
  └── [ OK ] test/Controller/ArtistControllerTest.php

Chapters