- 1. Understanding Stubs
- 2. Publishing the Stubs
- 3. Customizing Stubs
- 4. Resetting Stubs
- 5. Sharing Custom Stubs
- 6. Keeping Your Stubs Updated
- 7. Advanced Customizations with Packages
Modern web development, especially in the world of Laravel, often involves repetitive tasks. Laravel's artisan make
commands are a godsend in this regard, helping to scaffold various components like controllers, migrations, and tests. But what if you could customize the output of these commands to better suit your project's specific needs? One example of wanting to do this is to enable soft deletes globally whenever using artisan make
.
In this article, we'll explore how to do just that by delving into publishing and updating Laravel stubs.
1. Understanding Stubs
In Laravel, stubs are simple template files used to generate various components via the artisan make
commands. They reside in the framework's core, providing the default structures for generated items.
2. Publishing the Stubs
Before customizing the stubs, we need to publish them, which will create a stubs
directory in your project root.
php artisan stub:publish
Upon running this command, Laravel will copy all stub files to a /stubs
directory in the root of your application. This allows you to easily customize the default scaffolding templates.
3. Customizing Stubs
Navigate to the stubs
directory in your application. Here, you'll find a variety of stub files. Let's say you want every new controller to come with a predefined set of methods. Open the controller.model.stub
or controller.plain.stub
depending on your needs. Modify the file to include the methods you desire.
For instance, if you want every controller to have a data()
method, add:
public function data() {
//
}
Now, every time you run php artisan make:controller MyController
, the resulting controller will contain this data
method.
4. Resetting Stubs
If you ever need to revert your stubs back to their original state, simply delete the custom stub you've modified. Laravel will then default back to the framework's core stubs for subsequent artisan make
commands.
5. Sharing Custom Stubs
Considering Laravel's thriving community, sharing resources, including custom stubs, is commonplace. If you've crafted a particularly useful set of stub customizations, consider packaging them up and sharing them with the community!
6. Keeping Your Stubs Updated
While customizing stubs can be powerful, it's essential to ensure compatibility with future Laravel releases. When updating Laravel, always check if any core stubs have changed and update your custom stubs accordingly.
7. Advanced Customizations with Packages
Some packages allow even more granular control over the stubs and the artisan make
command's output. If you find yourself needing advanced customization, consider exploring packages like Laravel Idea or Blueprint.
Laravel's artisan make
commands are all about speeding up and simplifying the development process. By understanding and customizing stubs, you can further enhance this experience, tailoring Laravel's scaffolding capabilities to match your unique project requirements.
Interested in proving your knowledge of this topic? Take the PHP Fundamentals certification.
PHP Fundamentals
Covering the required knowledge to create and build web applications in PHP.
$99