Laravel has a number of useful little make commands. These allow you to generate a new class based off a template but how easy is it to make your own command to do this? Not too surprisingly this is actually really easy but it isn’t something you’ll find documented.
We’re going to firstly start by using a make command to make our new make command. This is done in the terminal with the artisan command:
php artisan make:command Make\\MakeClassCommand
This will have then created a new command class in the app/Console/Commands/Make folder, if everything worked then we should have a MakeClassCommand.php file there.
We now need to customise our command to make new classes. The first part to this is to extend the Illuminate\Console\GeneratorCommand class instead of the default Command class. This is a class in the Laravel framework which all the make commands inherit from. This reduces the work to only editing a adding a few methods and properties. We can begin by removing the handle method and the constructor methods along with the signature property, we won’t need these as the GeneratorCommand class already has them.
The next step will be to add a few properties to the class. Namely we need a name and type property. The name is simply the command’s signature but without any arguments. The type is simply used for a few bits of console output like the message saying ‘{type} has been successfully created’. Our class should currently look like this:
Now we need to add a few of our own methods. The first is the getDefaultNamespace() method. This will tell the command where in our applications PSR-4 namespace to create the new class file. In the case of the command we’re making we’re going to just keep to the normal root namespace, but if we wanted to we could return a namespace like so:
return $rootnamespace . ‘\Controllers’;
And then our command would put it under the app/Controllers folder.
The other method we can add is getStub() which provides a file path to our template class file. Now if you’ve dug into the Laravel framework before, these templates are normally stored in the namespace inside a stubs folder but I personally feel this is a bit messy so instead we’re going to adds a stubs folder to default Laravel resources folder, resources/stubs.
This ends up with our make command looking like the following:
As you can see from the example we’ll need to add a ClassStub.php to the stub folder. Our stub file will then need to be like the following:
You might ask, why DummyClass and DummyNamespace? Well this is because they are both placeholders that the make command will swap out when creating the new class.
Now we can go back to the artisan command and see the command in the console as well as create a new class with it.
Then we can use the command to make a class:
php artisan make:class ExampleClass
We should now end up with the following file located at app/ExampleClass.php.
Further Customisation
If you need to do more complicated things with your make command you can do so by overriding the inherited buildClass method of the GeneratorCommand class. I won’t cover this for now but potentially will in a further article. With this in mind you can really start to make your own Make commands which is a great way to simply tasks if you find yourself making Class files over and over again, it’s also a really good practise if you’re making a package for Laravel that will require the developer to make new files that conform to a particular pattern or workflow.
If you wish to view the end result as a code repo you can find it on GitHub.
I’m Peter Fox, a software developer in the UK who works with Laravel among other things. If you want to know more about me you can at https://www.peterfox.me and feel free to follow me @SlyFireFox on twitter for more Laravel tips and tutorials.
Top comments (0)