how do i create a controller inside a folder programmatically using the Spark make command ?
i wrote this code:
$controllerName = service('request')->getpost('controllerName');
$controllerPath = service('request')->getpost('controllerPath');
$controllerFullPath = APPPATH.'Views/'.$controllerPath;
if (!is_dir($viewFullPath)) {
mkdir($viewFullPath, 0777, true);
}
$output = shell_exec('php spark make:controller '.$viewPath.'/'.$viewName);
print_r($output);die;
the output :
the folder will be created but no controller file.
2
Answers
To create a controller inside a folder using the spark
make:controller
command in CodeIgniter 4, you need to structure the command correctly. Here’s how you can do it:Correct Command Syntax
Use a backslash () to specify the folder and controller name. For example:
If you want to create a controller named UserController inside a folder Admin:
We can create with three different ways:
Programmatically Generate:
Use PHP code to dynamically create a file in
app/Controllers
,writing the necessary PHP controller code as a string to the file.
Manually Creating the File:
Create a PHP file in the
app/Controllers
directory and write thecontroller class manually, extending
CodeIgniterController
.Using Spark CLI:
Run
php spark make:controller Name
in the terminal to generate acontroller named
Name
in theapp/Controllers
directory.