InfyOmLabs / laravel-generator

API and Admin Panel CRUD Generator for Laravel.
https://www.infyom.com/open-source
MIT License
3.79k stars 812 forks source link

[Feature]: add mechanism for adding our own dynamic variables to be used in customized templates #356

Closed vesper8 closed 2 years ago

vesper8 commented 7 years ago

In GeneratorConfig.php at the bottom of the loadDynamicVariables function you have this

        $commandData->addDynamicVariable(
            '$API_PREFIX$',
            config('infyom.laravel_generator.api_prefix', 'api')
        );

        $commandData->addDynamicVariable(
            '$API_VERSION$',
            config('infyom.laravel_generator.api_version', 'v1')
        );

This is a useful way of adding new dynamic variables. I have added a few of them myself that I use in my customized stubs.

However this forces me to make edits inside of the /vendor folder which is bad practice.

Would it be possible to add an array "dynamic_variables" inside of laravel_generator.php and iterate through it in GeneratorConfig.php so that we could add our own dynamic variables via the configuration file?

Please and thank you :)

mitulgolakiya commented 7 years ago

@vesper8 currently, there is no way to do this, I will take it in a queue and will add a support for it.

thewebartisan7 commented 4 years ago

This would be good idea. Any update on this?

mitulgolakiya commented 4 years ago

There is no update on this. We will think to add it soon.

mitulgolakiya commented 2 years ago

Just added support for this in the latest version v5.3.0.

You can add dynamic variables to the generator config by calling addDynamicVariables method in the service provider's boot method. For e.g,

Add the following code in the AppServiceProvider boot method,

GeneratorConfig::addDynamicVariable('testVar', 'testVal');

Alternatively, you can add multiple variables as well by,

GeneratorConfig::addDynamicVariables(['testVar1' => 'testVal1', 'testVar2' => 'testVal2']);

Once variables are added, you can publish templates and then modify the view file to use the variable,

protected $dynamicVars = [
    'testVal' => '{{ $config->getDynamicVariable('testVar') }}',
];

As per the new release, $config is injected in every view stub file that the generator uses.

Let me know if any further explanations or suggestions are there.