Skip to content

Commit 41dd866

Browse files
committed
Added routes command line
1 parent 2cfa794 commit 41dd866

5 files changed

Lines changed: 49 additions & 1 deletion

File tree

bin/src/ControllerCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ protected function execute(InputInterface $input, OutputInterface $output){
3737
}else{
3838
$result = $make->makeController($input);
3939

40-
$output->writeln('< fg=green >Your controller has been created in app/Controllers/ </>' . $input.'.php');
40+
$output->writeln('<fg=green>Your controller has been created in app/Controllers/</>' . $input.'.php');
4141

4242
}
4343

bin/src/Make.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,36 @@ public static function makeController( $controllerName ){
4444
fwrite($controllerConfigHandle, "\n". $controllerConfigTemplate);
4545
fclose($controllerConfigHandle);
4646

47+
48+
//update routes file
49+
//get a sample copy of the text to be written to the file config
50+
$routesTemplate = file_get_contents (__DIR__ .'/templates/Routes.php');
51+
$routesTemplate = str_replace('SamplesController',$controllerName, $routesTemplate);
52+
53+
//remove the Controller in the controller name
54+
//so SamplesController becomes Samples
55+
$databaseTableName = str_replace("Controller", "", $controllerName);
56+
//convert to lower case
57+
$databaseTableName = strtolower($databaseTableName);
58+
59+
//replace all occurences of samples
60+
$routesTemplate = str_replace('samples',$databaseTableName, $routesTemplate);
61+
62+
63+
//remove the <?php opening tag in the template file
64+
$routesTemplate = str_replace('<?php','', $routesTemplate);
65+
66+
//find the routes file
67+
$routesFile = __DIR__ . '/../../routes/routes.php';
68+
69+
$routesHandle = fopen($routesFile,"a") or die('Cannot open file: '.$routesFile);
70+
71+
//append to it
72+
fwrite($routesHandle, "\n". $routesTemplate);
73+
fclose($routesHandle);
74+
75+
76+
4777

4878
return true;
4979
}

bin/src/templates/Routes.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
//samples routes
3+
$app->get('/samples/index', 'SamplesController:index')->setName('samples.index'); //Optional user_id parameter
4+
$app->map(['POST', 'GET'], '/samples/add/', 'SamplesController:add')->setName('samples.add');
5+
$app->map(['POST', 'GET'], '/samples/edit/{id}', 'SamplesController:edit')->setName('samples.edit');
6+
$app->get('/samples/view/{id}', 'SamplesController:view')->setName('samples.view');
7+
$app->get('/samples/delete/{id}', 'SamplesController:delete')->setName('samples.delete');
8+

config/ControllerConfig.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,12 @@
3333

3434
$container['TestsController.'] = function($container){
3535
return new \App\Controllers\TestsController.($container);
36+
};
37+
38+
$container['TesterresController'] = function($container){
39+
return new \App\Controllers\TesterresController($container);
40+
};
41+
42+
$container['Testerre2sController'] = function($container){
43+
return new \App\Controllers\Testerre2sController($container);
3644
};

routes/routes.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,5 @@
7373

7474

7575
})->add(new AuthMiddleware($container));
76+
77+

0 commit comments

Comments
 (0)