> For the complete documentation index, see [llms.txt](https://docs.cmsfullform.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cmsfullform.com/documents/commands/controllers-command.md).

# Controllers Command

`ControllersCommand.php` defines a command for creating a new **Controller** file in the application. It ensures that a controller does not already exist, generates a basic controller template, and writes it to the appropriate directory. Additionally, it **prompts the user** to create a corresponding model if needed.

## `create()` in `ControllersCommand`

```php
public function create($name) {
        $controllerPath = ROOT_PATH . '/application/Controllers/' . ucfirst($name) . 'Controller.php';

        // Check if controller already exists
        if (file_exists($controllerPath)) {
            echo "Controller $name already exists.\n";
            return;
        }

        // Basic controller template
        $controllerContent = "<?php\n";
        $controllerContent .= "namespace App\Controllers;\n\n";
        $controllerContent .= "use System\Core\BaseController;\n\n";
        $controllerContent .= "class " . ucfirst($name) . "Controller extends BaseController {\n\n";
        $controllerContent .= "    public function index() {\n";
        $controllerContent .= "        echo 'Hello from " . ucfirst($name) . "Controller!';\n";
        $controllerContent .= "    }\n";
        $controllerContent .= "}\n";

        // Write the controller file
        file_put_contents($controllerPath, $controllerContent);
        echo "Controller $name created at $controllerPath.\n";

        // Prompt for model creation
        echo "Do you want to create a model for $name? (y/n): ";
        $response = trim(fgets(STDIN));
        if (strtolower($response) == 'y') {
            $modelCommand = new ModelsCommand();
            $modelCommand->create($name);
        }
    }
```

Example:

If you want to create a new Controller and named as 'User', run the following command:

```
php init controllers user
```

If `UserController.php` is exist:

```php
Controller User already exists.
```

If `UserController.php` is not exist, it will be generated:

```php
<?php
namespace App\Controllers;

use System\Core\BaseController;

class UserController extends BaseController {

    public function index() {
        echo 'Hello from UserController!';
    }
}
```

```
// Then you will see the message:
Controller User created at /root/application/Controllers/UserController.php.
Do you want to create a model for User? (y/n):

// If you enter "y":
Model UserModel created at /root/application/Models/UserModel.php.
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.cmsfullform.com/documents/commands/controllers-command.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
