Skip to content

Commit 5984988

Browse files
committed
✨ add non module controllers to be created with console command
Signed-off-by: otengkwame <[email protected]>
1 parent 9e255f4 commit 5984988

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

Core/controllers/commands/Create.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,44 @@ public function createController($location = '', $controllerName = '', $addContr
863863
}
864864
}
865865

866+
/**
867+
* Create A Non Module Controller
868+
*
869+
* @param string $controllerName
870+
* @param string $addController
871+
* @param string $location
872+
* @return void
873+
*/
874+
public function createNonModuleController($controllerName = '', $addController = '', $location = 'App/Controllers')
875+
{
876+
$created = '';
877+
878+
$controllerName = ucwords($controllerName);
879+
$fileType = "web_controller";
880+
881+
if ($addController == '--addcontroller') {
882+
$controllerName = Inflector::singularize($controllerName) . 'Controller';
883+
}
884+
885+
$this->controllers = 'Controllers';
886+
$controllerDirectory = $this->createAppRootDirectory($this->controllers);
887+
888+
if (file_exists($controllerDirectory . DS . $controllerName . $this->fileExtention)) {
889+
$this->failureOutput(ucfirst($controllerName). " Controller exists already in the " . $location . " directory");
890+
return;
891+
}
892+
893+
if ($controllerDirectory && is_dir($controllerDirectory)) {
894+
$filePath = $controllerDirectory . DS . $controllerName;
895+
$created = $this->createFile($filePath, $fileType, $this->controller);
896+
}
897+
898+
if ($created) {
899+
$this->successOutput(ucfirst($controllerName) . " Controller created successfully ");
900+
return;
901+
}
902+
}
903+
866904
/**
867905
* Create Model
868906
*
@@ -1401,7 +1439,7 @@ public function createView($location = '', $viewFile = '', $defaultType = '--emp
14011439
$moduleName = '';
14021440
$moduleType = '';
14031441
$created = '';
1404-
$viewFile = str_replace('::', '/', $viewFile);
1442+
$viewFile = str_replace('-', '/', $viewFile);
14051443

14061444
$pathinfo = (object) pathinfo($viewFile);
14071445

Core/core/Console/Console.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,13 @@ protected static function createController(...$args)
345345
$module = $args[0];
346346
$controllerName = '';
347347

348+
$nonModuleController = str_contains($args[0], '--name');
349+
350+
if ($nonModuleController) {
351+
$args[2] = $args[1];
352+
$args[1] = $args[0];
353+
}
354+
348355
$controller = str_replace('=', ':', $args[1]);
349356
$controller = explode(':', $controller);
350357

@@ -364,6 +371,12 @@ protected static function createController(...$args)
364371
if (isset($args[2])) {
365372
$addController = $args[2];
366373
}
374+
375+
if ($nonModuleController) {
376+
$command = Console::phpCommand() . 'create/createnonmodulecontroller/' . $controllerName . '/' . $addController;
377+
static::runSystemCommand($command);
378+
return;
379+
}
367380

368381
$module = str_replace('=',':', $module);
369382
$command = Console::phpCommand() . 'create/createcontroller/' . $module . '/' . $controllerName . '/' . $addController;

0 commit comments

Comments
 (0)