求助:Laravel如何通过php artisan基于自定义Stub创建Model
Got it, let's walk through exactly how to set this up so you can generate models that extend your custom MyParent class, include dynamic documentation, and have your custom methods right out of the box.
Step 1: Create Your Custom Model Stub
First, we need to define the template (stub) our new models will use. If you don't already have a stubs directory in your project root, create one. Then add a file named model.custom.stub with this content:
namespace {{ namespace }}; use App\Models\MyParent; /** * {{ dynamicDoc }} */ class {{ class }} extends MyParent { /*put custom methods here*/ }
We added a {{ dynamicDoc }} placeholder here that we'll replace with dynamic content later.
Step 2: Create a Custom Make Model Command
Next, we'll build our own Artisan command that uses this custom stub. Run this in your terminal to generate the command skeleton:
php artisan make:command CustomModelMakeCommand
Open the generated file at app/Console/Commands/CustomModelMakeCommand.php and update it to extend Laravel's default ModelMakeCommand, override the stub path, and add dynamic content replacement:
<?php namespace App\Console\Commands; use Illuminate\Foundation\Console\ModelMakeCommand as BaseModelCommand; use Illuminate\Support\Str; class CustomModelMakeCommand extends BaseModelCommand { // Define our command signature (name it whatever makes sense for your workflow) protected $signature = 'make:custom-model {name : The name of the model}'; protected $description = 'Create a new model using our custom stub that extends MyParent'; // Point to our custom stub file protected function getStub() { return base_path('stubs/model.custom.stub'); } // Override the class building method to inject dynamic content protected function buildClass($name) { // Get the default generated stub content $stub = parent::buildClass($name); // Extract the model class name (e.g., "TestModel" from "CustomNamespace\TestModel") $modelName = Str::afterLast($name, '\\'); // Replace our dynamic doc placeholder with a custom, context-aware message $stub = str_replace('{{ dynamicDoc }}', "Model representing the $modelName entity", $stub); return $stub; } }
Step 3: Register the Custom Command
We need to tell Laravel about our new command. Open app/Console/Kernel.php and add our command to the $commands array:
protected $commands = [ \App\Console\Commands\CustomModelMakeCommand::class, ];
Step 4: Generate Your Custom Model
Now you're ready to use your new command! Run this in your terminal:
php artisan make:custom-model CustomNamespace\TestModel
This will generate a model at app/Models/CustomNamespace/TestModel.php with exactly the structure you wanted:
namespace App\Models\CustomNamespace; use App\Models\MyParent; /** * Model representing the TestModel entity */ class TestModel extends MyParent { /*put custom methods here*/ }
If you want to adjust the dynamic documentation or add more custom placeholders later, just update the stub file and the buildClass method in your custom command to handle those replacements.
内容的提问来源于stack exchange,提问作者4givN

