Skip to content

Commit e238ca1

Browse files
committed
=Releases v2.1 for production
1 parent 3e45f80 commit e238ca1

16 files changed

+269
-156
lines changed

README.md

Lines changed: 59 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# An awesome code generator for laravel framework - with client-side validation
22

3-
For full documentation and live demo please visit <a href="https://crestapps.com/laravel-code-generator/docs/2.0" target="_blank" title="Laravel Code Generator Documentation">CrestApps.com</a>
3+
For full documentation and live demo please visit <a href="https://crestapps.com/laravel-code-generator/docs/2.1" target="_blank" title="Laravel Code Generator Documentation">CrestApps.com</a>
44

55
## Introduction
66

@@ -31,10 +31,6 @@ A clean code generator for Laravel framework that will save you time! This aweso
3131
* Create a very clean and reusable code.
3232
* Lots of documentation.
3333

34-
## Prologue
35-
* <a href="https://crestapps.com/laravel-code-generator/docs/2.0#release-notes">Release Notes</a>
36-
* <a href="https://crestapps.com/laravel-code-generator/docs/2.0#upgrade-guide">Upgrade Guide</a>
37-
3834
## Installation
3935

4036
To download this package into your laravel project, use the command-line to execute the following command
@@ -43,7 +39,7 @@ A clean code generator for Laravel framework that will save you time! This aweso
4339
composer require crestapps/laravel-code-generator --dev
4440
```
4541

46-
To bootstrap the packages into your project, open the `config/app.php` file in your project. Then, look for the providers array.
42+
**(Skip this step when using Laravl >= 5.5)** To bootstrap the packages into your project, open the `config/app.php` file in your project. Then, look for the providers array.
4743

4844
Add the following line to bootstrap laravel-code-generator to the framework.
4945

@@ -58,34 +54,78 @@ php artisan vendor:publish --provider="CrestApps\CodeGenerator\CodeGeneratorServ
5854

5955
> A layout is required for the default views! The code generator allows you to create a layout using the command-line. Of cource you can use your own layout. You'll only need to include [CSS bootstrap framework](http://getbootstrap.com/ "CSS bootstrap framework") in your layout for the default templates to work properly. Additionally, you can chose to you design your own templetes using a different or no css framework.
6056
61-
6257
## Available Commands
6358

6459
> The command in between the square brackets [] must be replaced with a variable of your choice.
6560
66-
* php artisan create:resources [model-name]
6761
* php artisan create:layout [application-name]
68-
* php artisan create:controller [controller-name]
62+
* php artisan create:resources [model-name]
63+
* php artisan create:mapped-resources
64+
* php artisan create:controller [model-name]
6965
* php artisan create:model [model-name]
70-
* php artisan create:routes [controller-name]
66+
* php artisan create:routes [model-name]
7167
* php artisan create:views [model-name]
7268
* php artisan create:create-view [model-name]
7369
* php artisan create:edit-view [model-name]
7470
* php artisan create:index-view [model-name]
7571
* php artisan create:show-view [model-name]
7672
* php artisan create:form-view [model-name]
77-
* php artisan create:migration [table-name]
78-
* php artisan create:form-request [class-name]
79-
* php artisan create:language [language-file-name]
80-
* php artisan create:fields-file [table-name]
81-
* php artisan fields-file:create [file-name]
82-
* php artisan fields-file:append [file-name]
83-
* php artisan fields-file:reduce [file-name]
73+
* php artisan create:migration [model-name]
74+
* php artisan create:form-request [model-name]
75+
* php artisan create:language [model-name]
76+
* php artisan create:fields-file [model-name]
77+
* php artisan fields-file:create [model-name]
78+
* php artisan fields-file:append [model-name]
79+
* php artisan fields-file:reduce [model-name]
80+
* php artisan fields-file:delete [model-name]
81+
82+
> Full documentation available at [CrestApps.com](https://www.crestapps.com/laravel-code-generator/docs/2.1 "Laravel Code Generator Documentation").
83+
84+
> Live demo is available at [CrestApps.com](https://www.crestapps.com/laravel-code-generator/demos/v2-1 "Laravel Code Generator Live Demo").
85+
86+
87+
## Examples
88+
89+
Lets create a CRUD called <var>AssetCategory</var> with the fields listed below.
8490

85-
> Full documentation available at [CrestApps.com](https://www.crestapps.com/laravel-code-generator/docs/2.0 "Laravel Code Generator Documentation").
91+
- id
92+
- name
93+
- description
94+
- is_active
8695

87-
> Live demo is available at [CrestApps.com](https://www.crestapps.com/laravel-code-generator/demos/2.0 "Laravel Code Generator Live Demo").
8896

97+
### Basic example
98+
99+
<blockquote>
100+
<p><code>php artisan fields-file:create AssetCategory --names=id,name,description,is_active</code></p>
101+
<p><small>The above command will create fields-file names <var>/resources/codegenerator-files/asset_categories.json</var></small></p>
102+
<p><code>php artisan create:resources AssetCategory</code></p>
103+
<p><small>The above command will create a model <var>app/Models/AssetCategory</var>, a controller <var>app/Http/Controllers/AsseyCategoriesController, all views, the routes, and migration file!</var></small></p>
104+
</blockquote>
105+
106+
107+
### Basic example using translations for english and arabic
108+
109+
<blockquote>
110+
<p><code>php artisan fields-file:create AssetCategory --names=id,name,description,is_active --translation-for=en,ar</code></p>
111+
<p><small>The above command will create fields-file names <var>/resources/codegenerator-files/asset_categories.json</var></small></p>
112+
<p><code>php artisan create:resources AssetCategory</code></p>
113+
<p><small>The above command will create a model <var>app/Models/AssetCategory</var>, a controller <var>app/Http/Controllers/AsseyCategoriesController, all views, the routes, and migration file!</var></small></p>
114+
</blockquote>
115+
116+
117+
### Creating resources from existing database with translation for english and arabic
118+
119+
<blockquote>
120+
<p><code>php artisan create:resources AssetCategory --table-exists --translation-for=en,ar</code></p>
121+
<p><small>The above command will create fields-file names <var>/resources/codegenerator-files/asset_categories.json</var></small></p>
122+
<p><small>Then it will create a model <var>app/Models/AssetCategory</var>, a controller <var>app/Http/Controllers/AsseyCategoriesController, all views and the routes!</var></small></p>
123+
<p><small>You may also create a fields-file from existing database separately using <code>create:fields-file AssetCategory --translation-for=en,ar</code></small></p>
124+
</blockquote>
125+
126+
## Prologue
127+
* <a href="https://crestapps.com/laravel-code-generator/docs/2.1#release-notes">Release Notes</a>
128+
* <a href="https://crestapps.com/laravel-code-generator/docs/2.1#upgrade-guide">Upgrade Guide</a>
89129

90130
## License
91131

src/Commands/CreateControllerCommand.php

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class CreateControllerCommand extends Command
3333
{--with-form-request : This will extract the validation into a request form class.}
3434
{--with-auth : Generate the controller with Laravel auth middlewear. }
3535
{--template-name= : The template name to use when generating the code.}
36+
{--form-request-directory= : The directory of the form-request.}
3637
{--controller-extends=Http\Controllers\Controller : The base controller to be extend.}
3738
{--force : This option will override the controller if one already exists.}';
3839

@@ -57,16 +58,6 @@ class CreateControllerCommand extends Command
5758
*/
5859
protected $requestVariable = '$request';
5960

60-
/**
61-
* Get the stub file for the generator.
62-
*
63-
* @return string
64-
*/
65-
protected function getStub()
66-
{
67-
return $this->getStubByName('controller', $this->getTemplateName());
68-
}
69-
7061
/**
7162
* Build the model class with the given name.
7263
*
@@ -77,7 +68,7 @@ protected function getStub()
7768
public function handle()
7869
{
7970
$input = $this->getCommandInput();
80-
$destenationFile = $this->getDestenationFile($input->controllerName);
71+
$destenationFile = $this->getDestenationFile($input->controllerName, $input->controllerDirectory);
8172

8273
if ($this->alreadyExists($destenationFile)) {
8374
$this->error('The controller already exists!');
@@ -90,18 +81,18 @@ public function handle()
9081

9182
if ($input->withFormRequest) {
9283
$requestName = $input->formRequestName;
93-
$requestNameSpace = $this->getRequestsNamespace($requestName);
84+
$requestNameSpace = $this->getRequestsNamespace($requestName, $input->formRequestDirectory);
9485
$this->makeFormRequest($input);
9586
}
9687

9788
$fields = $this->getFields($input->fields, $input->langFile, $input->fieldsFile);
9889
$viewVariablesForIndex = $this->getCompactVariablesFor($fields, $this->getPluralVariable($input->modelName), 'index');
9990
$viewVariablesForShow = $this->getCompactVariablesFor($fields, $this->getSingularVariable($input->modelName), 'show');
10091
$viewVariablesForEdit = $this->getCompactVariablesFor($fields, $this->getSingularVariable($input->modelName), 'form');
101-
$modelFullName = $this->getModelFullName($input->modelDirectory, $input->modelName);
92+
$modelNamespace = $this->getModelNamespace($input->modelName, $input->modelDirectory);
10293
$affirmMethod = $this->getAffirmMethod($input->withFormRequest, $fields, $requestNameSpace);
10394
$classToExtendFullname = $this->getFullClassToExtend($input->extends);
104-
$namespacesToUse = $this->getRequiredUseClasses($fields, [$modelFullName, $requestNameSpace, $classToExtendFullname]);
95+
$namespacesToUse = $this->getRequiredUseClasses($fields, [$modelNamespace, $requestNameSpace, $classToExtendFullname]);
10596
$dataMethod = $this->getDataMethod($fields, $requestNameSpace . '\\' . $requestName, $input->withFormRequest);
10697
$stub = $this->getStubContent('controller');
10798
$languages = array_keys(Helpers::getLanguageItems($fields));
@@ -112,7 +103,7 @@ public function handle()
112103
->replaceGetDataMethod($stub, $dataMethod)
113104
->replaceCallDataMethod($stub, $this->getCallDataMethod($input->withFormRequest))
114105
->replaceModelName($stub, $input->modelName)
115-
->replaceNamespace($stub, $this->getControllersNamespace())
106+
->replaceNamespace($stub, $this->getControllersNamespace($input->controllerDirectory))
116107
->replaceControllerExtends($stub, $this->getControllerExtends($classToExtendFullname))
117108
->replaceUseCommandPlaceholder($stub, $namespacesToUse)
118109
->replaceRouteNames($stub, $this->getModelName($input->modelName), $input->prefix)
@@ -611,37 +602,50 @@ protected function getUploadFileMethod(array $fields, $withFormRequest)
611602
}
612603

613604
/**
614-
* Gets the controller's fullname
605+
* Gets the destenation file to be created.
606+
*
607+
* @param string $name
608+
* @param string $path
615609
*
616610
* @return string
617611
*/
618-
protected function getDestenationFile($name)
612+
protected function getDestenationFile($name, $path)
619613
{
620-
$path = app_path(Config::getControllersPath());
621614

622-
return Helpers::postFixWith($path, '/') . $name . '.php';
615+
if (!empty($path)) {
616+
$path = Helpers::getPathWithSlash(ucfirst($path));
617+
}
618+
619+
return app_path(Config::getControllersPath($path. $name . '.php'));
623620
}
624621

625622
/**
626623
* Gets the Requests namespace
627624
*
625+
* @param string $name
626+
* @param string $path
627+
*
628628
* @return string
629629
*/
630-
protected function getRequestsNamespace($name)
630+
protected function getRequestsNamespace($name, $path)
631631
{
632-
$path = $this->getAppNamespace() . Config::getRequestsPath();
632+
$path = str_finish($path, '\\');
633+
634+
$path = $this->getAppNamespace() . Config::getRequestsPath($path);
633635

634636
return Helpers::convertSlashToBackslash($path) . $name;
635637
}
636638

637639
/**
638640
* Gets the controllers namespace
639641
*
642+
* @param string $path
643+
*
640644
* @return string
641645
*/
642-
protected function getControllersNamespace()
646+
protected function getControllersNamespace($path)
643647
{
644-
$path = $this->getAppNamespace() . Config::getControllersPath();
648+
$path = $this->getAppNamespace() . Config::getControllersPath($path);
645649

646650
return rtrim(Helpers::convertSlashToBackslash($path), '\\');
647651
}
@@ -673,31 +677,34 @@ protected function makeFormRequest($input)
673677
{
674678
$this->callSilent('create:form-request',
675679
[
676-
'model-name' => $input->modelName,
677-
'--class-name' => $input->formRequestName,
678-
'--fields' => $input->fields,
679-
'--force' => $input->force,
680-
'--with-auth' => $input->withAuth,
681-
'--fields-file' => $input->fieldsFile,
682-
'--template-name' => $input->template
680+
'model-name' => $input->modelName,
681+
'--class-name' => $input->formRequestName,
682+
'--fields' => $input->fields,
683+
'--force' => $input->force,
684+
'--with-auth' => $input->withAuth,
685+
'--fields-file' => $input->fieldsFile,
686+
'--template-name' => $input->template,
687+
'--form-request-directory' => $input->formRequestDirectory
683688
]);
684689

685690
return $this;
686691
}
687692

688693
/**
689-
* Gets the full model name
694+
* Gets the namespace of the model
690695
*
691-
* @param string $directory
692-
* @param string $name
696+
* @param string $modelName
697+
* @param string $modelDirectory
693698
*
694699
* @return string
695700
*/
696-
protected function getModelFullName($directory, $name)
701+
protected function getModelNamespace($modelName, $modelDirectory)
697702
{
698-
$final = !empty($directory) ? Config::getModelsPath() . Helpers::getPathWithSlash($directory) : Config::getModelsPath();
703+
$modelDirectory = str_finish($modelDirectory, '\\');
704+
705+
$namespace = $this->getAppNamespace() . Config::getModelsPath($modelDirectory . $modelName);
699706

700-
return Helpers::convertSlashToBackslash($this->getAppNamespace() . $final . $name);
707+
return rtrim(Helpers::convertSlashToBackslash($namespace), '\\');
701708
}
702709

703710
/**
@@ -710,7 +717,6 @@ protected function getCommandInput()
710717
$modelName = trim($this->argument('model-name'));
711718
$cName = trim($this->option('controller-name'));
712719
$controllerName = $cName ? str_finish($cName, 'Controller') : Helpers::makeControllerName($modelName);
713-
;
714720
$viewDirectory = $this->option('views-directory');
715721
$prefix = $this->option('routes-prefix');
716722
$perPage = intval($this->option('models-per-page'));
@@ -720,14 +726,16 @@ protected function getCommandInput()
720726
$withFormRequest = $this->option('with-form-request');
721727
$force = $this->option('force');
722728
$modelDirectory = $this->option('model-directory');
729+
$controllerDirectory = trim($this->option('controller-directory'));
723730
$formRequestName = Helpers::makeFormRequestName($modelName);
724731
$template = $this->getTemplateName();
732+
$formRequestDirectory = trim($this->option('form-request-directory'));
725733
$extends = $this->generatorOption('controller-extends');
726734
$withAuth = $this->option('with-auth');
727735

728-
return (object) compact('viewDirectory', 'viewName', 'modelName', 'prefix', 'perPage', 'fileSnippet', 'modelDirectory',
736+
return (object) compact('formRequestDirectory','viewDirectory', 'viewName', 'modelName', 'prefix', 'perPage', 'fileSnippet', 'modelDirectory',
729737
'langFile', 'fields', 'withFormRequest', 'formRequestName', 'force', 'fieldsFile', 'template',
730-
'controllerName', 'extends', 'withAuth');
738+
'controllerName', 'extends', 'withAuth','controllerDirectory');
731739
}
732740

733741
/**

0 commit comments

Comments
 (0)