MVC For MVC projects (see the flavor table in Overview).
In an MVC project, make:module writes a module: a part of the application with its own controllers, models and views, for example admin or reports. All the modules are in the module path of crest.php (src/Modules), and each module has the same structure:
crest make:module adminCreated src/Modules/Admin/Module.php
Created src/Modules/Admin/Controllers
Created src/Modules/Admin/Models
Created src/Modules/Admin/views| Path | Contents |
|---|---|
src/Modules/Admin/Module.php |
App\Modules\Admin\Module, which implements Phalcon\Mvc\ModuleDefinitionInterface |
src/Modules/Admin/Controllers/ |
the controllers (crest make:controller Index --module=admin) |
src/Modules/Admin/Models/ |
the models (crest make:model users --module=admin) |
src/Modules/Admin/views/ |
the views of the module |
After the Created lines, Crest prints the lines to add to your front controller and your router (see Registration and routes).
The folder is the name with the first letter of each part between _ in uppercase: admin and Admin give Admin, and admin_panel gives AdminPanel. The key of the module is the folder in lowercase: admin, or adminpanel. You use the key in registerModules(), in the routes and in --module. Crest finds a module from its key, so --module=adminpanel finds the folder AdminPanel. A name that does not give a class name (my module, admin-panel) stops the command.
Module.php is yours: add the services of the module in registerServices(). Crest writes it one time. When it exists, make:module stops; --force replaces only Module.php, and the folders and their files stay. There is no config file. A module that needs settings reads them from the environment in registerServices(), as DbProvider does in a project with a database. With docker, docker compose gives the values of .env to the app container. PHP on the host does not read .env (see The database).
Registration and routes
Crest does not change your front controller or your router. run() of AppFront handles the request and sends the response. Register the module in run(), before handle():
public function run(): int
{
$uri = (string) ($_SERVER['REQUEST_URI'] ?? '/');
$application = new Application($this->boot());
$application->registerModules([
'admin' => ['className' => \App\Modules\Admin\Module::class],
'common' => ['className' => \App\Modules\Common\Module::class],
]);
$response = $application->handle($uri);
if ($response instanceof ResponseInterface) {
$response->send();
}
return 0;
}Then send requests to the modules with routes. Write a provider for the router (crest make:provider Router), and add the line that it prints (new \App\Providers\RouterProvider(),) to providers():
public function register(DiInterface $di): void
{
$di->setShared('router', function (): Router {
// The default routes stay for the controllers of the project.
$router = new Router();
$router->add('/admin', [
'module' => 'admin',
'controller' => 'index',
'action' => 'index',
]);
$router->add('/admin/:controller', [
'module' => 'admin',
'controller' => 1,
'action' => 'index',
]);
$router->add('/admin/:controller/:action/:params', [
'module' => 'admin',
'controller' => 1,
'action' => 2,
'params' => 3,
]);
// A route that does not follow the pattern of a module.
$router->add('/login', [
'module' => 'common',
'controller' => 'session',
'action' => 'login',
]);
return $router;
});
}Router is Phalcon\Mvc\Router. A request with no module in its route uses the dispatcher and the views of the project, as before.
What a module changes
registerServices() of Module.php changes two services that the providers of the project set:
- the default namespace of the dispatcher becomes the namespace of the controllers of the module (
App\Modules\Admin\Controllers); - the views folder becomes the
views/folder of the module.
Thus a page of the module uses only the views of the module, and shows no layout of the project. Add a layout in the views/ folder of the module. For one views folder for the whole application, change the setViewsDir() line of Module.php to the views folder of the project.
In a project with no views ('views' => false), the module has no views/ folder, and Module.php changes only the dispatcher. The printed registration also calls useImplicitView(false), as the front controller of the project does.
A project with no dispatcher or view service must change registerServices().
Models in a module
A relation of a model in a module names a model in the same module. To share models between modules, keep them in the model path: run make:model with no --module. make:model --module reads the tables through the db service of the project, also when the module sets its own db.