---
title: "Generating Controllers"
description: "Generate the controllers of an MVC project with make:controller."
version: "1.0"
---

> Documentation Index
> Fetch the complete documentation index at: https://crest.phalcon.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Generating Controllers

**MVC** For MVC projects (see the flavor table in [Overview](/1.0/introduction)).

In an MVC project, `make:controller` writes a controller and the base class that it extends (see [Generated bases](#generated-bases)):

```bash
crest make:controller Users
```

When the base does not exist yet, the output starts with these lines:

```text
Created src/Controllers/Base/CrestControllerBase.php
Created src/Controllers/UsersController.php
```

In the `Created`, `Updated` and `Kept` lines of the `make:` commands, Crest prints the full path of each file or folder. The examples of these lines in this document show the path from the project root.

`UsersController` extends `CrestControllerBase`, which extends `Phalcon\Mvc\Controller`, and has an empty `indexAction()`. With the default routes of the Phalcon router, the first part of the URL names the controller. Crest then prints what the dispatcher needs: its default namespace, the URL of `indexAction()` with the default routes (`GET /users`), and the name of its view (`users/index`). The views folder and the engine belong to your project. In a project with no views (`'views' => false`), Crest names no view: the action returns its response.

A project from `crest new --flavor=mvc` already has `CrestControllerBase` and `IndexController`, and its dispatcher has the default namespace. Thus in that project, `make:controller` prints no line for the base while the base is the same as its stub (see [Generated bases](#generated-bases)).

The name must give a class that the dispatcher can find from a URL. Crest stops on `users` (the URL `/users` looks for `UsersController`), on `User_Profiles`, and on a name that gives the suffix two times in the class name, for example `UserControllerController`, or `Usercontroller` (which gives `UsercontrollerController`). `UserController` gives `UserController`.

To write the controller into a module, add `--module` (see [Generating modules](/1.0/modules)):

```bash
crest make:controller Users --module=admin
```

```text
Created src/Modules/Admin/Controllers/Base/CrestControllerBase.php
Created src/Modules/Admin/Controllers/UsersController.php
```

Each module has its own `CrestControllerBase`. The `Module.php` of the module sets the default namespace of the dispatcher. Thus Crest prints the URL with the routes of `make:module` (`GET /admin/users/index`) and the views folder of the module. The module must exist. If it does not exist, Crest stops and tells you to run `make:module` first.

## Generated bases

Crest owns `CrestControllerBase` in `src/Controllers/Base/`. Its docblock starts with `Generated by crest.` Do not edit it: when you run `make:controller` or `make:scaffold`, Crest compares the file with the stub of the base. If they are not the same, Crest writes the file again. This removes your changes. You own the controllers, and Crest writes each one only once. Each module has its own copy, in the `Controllers/Base/` folder of the module (`src/Modules/Admin/Controllers/Base/`), with the same rules. For the new base of a module, add `--module=<key>` to `make:controller`, with the key of the module (see [Generating modules](/1.0/modules)).

- To get the new base after an upgrade of Crest, run `make:controller` again on an existing controller. Crest keeps the controller and writes the base:

  ```text
  Updated src/Controllers/Base/CrestControllerBase.php
  Kept src/Controllers/UsersController.php: it exists; pass --force to replace it
  ```

- `--force` replaces the controller with a new one. It never replaces a base without the `Generated by crest.` line: Crest keeps that file and prints a note. To get the Crest base back, delete the file or add the `Generated by crest.` line to it.
- Put the code that your controllers share into your own class that extends `CrestControllerBase`. Publish `controller.stub` (`crest stub:publish controller`) and change its `use` line and its `extends`, so that new controllers extend your class.
- Crest uses a published `controller-base.stub`, not the packaged one. Thus the upgrades of Crest no longer change your base. Keep the `Generated by crest.` line in your copy. Then Crest writes the base again when you change the stub.

Source: https://crest.phalcon.io/1.0/controllers/index.mdx
