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

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:

If UserController.php is exist:

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

Last updated

Was this helpful?