# 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: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

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

The question should be specific, self-contained, and written in natural language.
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.
