Laravel: Making make commands
Tags: php, laravel
Published: 2019-03-15 12:50:58

---
title: Laravel: Making make commands
published: true
tags: php,laravel
canonical_url: 
---

![](https://cdn-images-1.medium.com/max/1024/0*VaU7iJlplz2ALO2d)<figcaption>Photo by <a href="https://unsplash.com/@nevenkrcmarek?utm_source=medium&amp;utm_medium=referral">Neven Krcmarek</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption>

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.

![](https://cdn-images-1.medium.com/max/934/1*NL1m7pRIhNvI0hIstOwUzw.png)<figcaption>Copy of the artisan make commands</figcaption>

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:

{% gist https://gist.github.com/peterfox/0bd727ca9ebc81b9c2b79527ea23aae9 %}

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:

{% gist https://gist.github.com/peterfox/1d731b60c1c078946e9e58de0268968b %}

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:

{% gist https://gist.github.com/peterfox/72ba21e72501363588481b0227eee8f7 %}

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.

![](https://cdn-images-1.medium.com/max/1024/1*2rUoQ7TMfQxgjTNY8GEPPQ.png)

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.

![](https://cdn-images-1.medium.com/max/1024/1*xRRa5-v81d4N_dg1cRMOMQ.png)

### 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](https://github.com/peterfox/make-command-demo).

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](https://www.peterfox.me) and feel free to follow me @[SlyFireFox](https://twitter.com/SlyFireFox) on twitter for more Laravel tips and tutorials.