5/8/2023 0 Comments Laravel makerIf you want to generate your file in the default namespace (usually "App"), you can safely ignore this. There's a single abstract method on the parent class that we'll have to define, getStub() and will return a path to the "stub" file, let's implement it to be protected function getStub()Ī stub file is a file that has the general shape of the final file we want to generate but with dummy placeholders so we can replace them with our desired values, for example, if we're generating a CreateUserAction class, the stub would have Dumm圜lass as a class name and we'll replace it with CreateUserAction when generating the final file. ![]() Let's change the command name to protected $name = 'make:action', and override protected $type to set it to 'Action' this will be used to output the message "Action already exists" if you tried generating the same action twice. Extendįirst, let's generate a command: php artisan make:command ActionMakeCommand but as I said we want it to extend Illuminate\Console\GeneratorCommand instead of the default Illuminate\Console\Command. ![]() ![]() Laravel has an abstract class that all make commands extend, and then override what's needed for the custom behavior, for example generating a Seeder is different than generating a Controller. ![]() In this post, I wanna show you how to create a class generator command in a very clean, Laravel-y way, if that didn't ring a bell, it's like the artisan make:* commands, let's dive in.įor our example we're gonna implement a make command to generate an "Action" class, an action class is a simple class that has a single handle() method and usually does one thing only.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |