Maslosoft Framework Logo Maslosoft Framework Documentation
Flexible Application Foundation


Defining Controller

Naming conventions

Convention over configuration allows predictable structure of classes in any module or application, allowing better coding performance, due the the fact, that the location of files is known, and configuration of each controller is not required.

Names and structure

Controller

To define controller for module, create class in Controllers directory relative to the module class. The class name must conform to naming convention - class name must end with Controller. The first part of controller class name is controller ID, which has first letter lower cased when referring to ID. All controller classes should inherit from Maslosoft\Components\Controller class to have all required features, access control and other filters. It is highly recommended to also define @Label annotation which will be displayed to end user and optionally translated to other languages. The class name should be in singular form, while label in plural.

Example of controller class name:

...
/**
 * @Label('User Messages')
 */
class UserMessagesController extends Controller
{
    ...
}

The resulting controller ID from above class will be named userMessages, and by default will be part of URL to access this controller.

Actions

The similar convention apply to the actions, which names must start with action and follow action name, from which resulting action ID will have first-letter-lower-cased equivalent. The action should contain label and access control list.

Example of controller action name:

...
class UserMessagesController extends Controller
{
...
    /**
     * @Label('All')
     * @Allowed('@', 'manager')
     */
    public function actionList(): void
    {
        $this->render('list');
    }
...
}
Views

The views should be contained in views subdirectory relative to controller class, and in view in another subdirectory named after controller ID. The file name can be declared arbitrarily, as it will be referenced by name when rendering in controller.

Example of view paths and file names for UserMessagesController controller:

views/userMessages/list.php
UserMessagesController.php