CoderFunda
  • Home
  • About us
    • Contact Us
    • Disclaimer
    • Privacy Policy
    • About us
  • Home
  • Php
  • HTML
  • CSS
  • JavaScript
    • JavaScript
    • Jquery
    • JqueryUI
    • Stock
  • SQL
  • Vue.Js
  • Python
  • Wordpress
  • C++
    • C++
    • C
  • Laravel
    • Laravel
      • Overview
      • Namespaces
      • Middleware
      • Routing
      • Configuration
      • Application Structure
      • Installation
    • Overview
  • DBMS
    • DBMS
      • PL/SQL
      • SQLite
      • MongoDB
      • Cassandra
      • MySQL
      • Oracle
      • CouchDB
      • Neo4j
      • DB2
      • Quiz
    • Overview
  • Entertainment
    • TV Series Update
    • Movie Review
    • Movie Review
  • More
    • Vue. Js
    • Php Question
    • Php Interview Question
    • Laravel Interview Question
    • SQL Interview Question
    • IAS Interview Question
    • PCS Interview Question
    • Technology
    • Other

23 April, 2021

Customizing Stubs in Laravel

 Programing Coderfunda     April 23, 2021     Packages, php     No comments   

 

Customizing Stubs in Laravel

This post will show you how to customize stubs used to generate various classes in your application. While a minor inconvenience, manually adjusting every generated class can be tedious, and Laravel provides a way for developers to publish and version stubs in an application if you want to suit generated classes to your specific taste.

If you want to follow along, you can create a new Laravel project with the Laravel installer, using Sail, or any other way you prefer to create a new application:

laravel new stub-demo --git

You might have noticed that the Laravel installer now supports Git and GitHub integration assuming you have the minimum git version required, you should have a new repository and a first commit.

Versioning our demo project is an excellent way to visualize the stub changes we make along the way and see what kind of files Laravel publishes to the app.

Publishing Stubs

The first step in customizing stubs could be to add stubs you'd like to customize individually to the /stubs folder at the root of a Laravel project, or you can publish all of them with Artisan:

$ php artisan stub:publish
$ git add stubs
$ git status
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

new file: stubs/cast.stub
new file: stubs/console.stub
new file: stubs/controller.api.stub
new file: stubs/controller.invokable.stub
new file: stubs/controller.model.api.stub
new file: stubs/controller.model.stub
new file: stubs/controller.nested.api.stub
new file: stubs/controller.nested.stub
new file: stubs/controller.plain.stub
new file: stubs/controller.stub
new file: stubs/factory.stub
new file: stubs/job.queued.stub
new file: stubs/job.stub
new file: stubs/middleware.stub
new file: stubs/migration.create.stub
new file: stubs/migration.stub
new file: stubs/migration.update.stub
new file: stubs/model.pivot.stub
new file: stubs/model.stub
new file: stubs/observer.plain.stub
new file: stubs/observer.stub
new file: stubs/policy.plain.stub
new file: stubs/policy.stub
new file: stubs/request.stub
new file: stubs/resource-collection.stub
new file: stubs/resource.stub
new file: stubs/rule.stub
new file: stubs/seeder.stub
new file: stubs/test.stub
new file: stubs/test.unit.stub

As you can see, we have quite a few stubs published in the app folder! I'll leave it up to you if you want to version all of them, but you could either keep a copy of them or only keep the specific stubs you want to customize.

Custom Controller Stubs

Laravel 8.36 introduced the idea of a --type flag when making a controller, allowing you to write custom stub files for generating a controller:

<?php
// stubs/controller.custom.stub
namespace {{ namespace }};

use {{ rootNamespace }}Http\Controllers\Controller;
use Illuminate\Http\Request;

/**
* Hello from the custom controller stub
*/
class {{ class }}
{
//
}

After adding the custom stub class, you can generate a controller using this template:

php artisan make:controller --type=custom MyController

Which would generate the following controller file:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

/**
* Hello from the custom controller stub
*/
class MyController
{
//
}

While this flexibility level is neat, I believe most developers can fit within the bounds of the stubs provided by the framework. Using the new --type flag is a manual way of picking which controller template you want to generate:

php artisan make:controller --type=plain PlainController

Which would generate a file based on the stubs/controller.plain.stub file published by Laravel:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PlainController extends Controller
{
//
}

A Sane Approach

Suppose you wanted to prepend a copyright comment to every file generated in your Laravel application. In that case, you could consider versioning all available stubs, and as you upgrade, run the stub:publish command to get newly added stubs.

For typical use-cases, though, perhaps you might only version the stubs you need to customize. For example, let's say that you don't want any controllers to extend the base Controller class; you could version all the controller.* stubs with your customizations but remove all the other stubs.

What If Stubs Change Upstream?

Let's say you version all the stubs from stub:publish, but you are concerned that as the Laravel framework receives updates to core stub files, your app will be out-of-date. If you version all stubs, you can always force an update to get the latest versions.

Take this for example, let's modify a stub and commit it to Git:

echo "/* test */" >> stubs/test.stub
git commit -am"Testing stub update"

You've updated the test stub and committed the update to Git. Let's say later Laravel publishes some updates to stubs and you want to verify if any have changed:

$ php artisan stub:publish --force
$ git diff
diff --git a/stubs/test.stub b/stubs/test.stub
index 834a53d..84c75cb 100644
--- a/stubs/test.stub
+++ b/stubs/test.stub
@@ -20,4 +20,3 @@ class {{ class }} extends TestCase
$response->assertStatus(200);
}
}
-/* test */

You have an easy way to see how your stubs have diverged from the Laravel codebase over time! Since the stubs are versioned you can simply undo changes the --force flag causes if you need to merge your changes with the latest stub changes.

  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook

Related Posts:

  • How to use datepicker in vuejs Example With Code Examples I want to learn you to use datepicker component in vue js using vuejs-datepicker picker. if you need to use datepicker in vue js then i will giv… Read More
  • How to get current date time in vue js With Code Examples Do you want to get current date in vue js app. If yes then i will help you to getting current date and time in vue js app. if you know to get cu… Read More
  • Laravel Vue Datatables Component Example With Code Examples In this tutorial, i want to share with you how to implement datatables with vue js laravel. i will share simple example of vue datatable in lara… Read More
  • Laravel Vue JS File Upload With Code Examples Today, we will learn file upload with laravel and vue js. we will create example of laravel vue axios file upload. we can easily fire post reque… Read More
  • Vue JS Ajax Form Submit With Code Examples I will share example of how to ajax form submit using api in vue js app. you can easily pass form data with ajax post request in vue.js. we will… Read More
Newer Post Older Post Home

0 comments:

Post a Comment

Thanks

Meta

Popular Posts

  • Spring boot app (error: method getFirst()) failed to run at local machine, but can run on server
    The Spring boot app can run on the online server. Now, we want to replicate the same app at the local machine but the Spring boot jar file f...
  • Log activity in a Laravel app with Spatie/Laravel-Activitylog
      Requirements This package needs PHP 8.1+ and Laravel 9.0 or higher. The latest version of this package needs PHP 8.2+ and Laravel 8 or hig...
  • Laravel auth login with phone or email
          <?php     Laravel auth login with phone or email     <? php     namespace App \ Http \ Controllers \ Auth ;         use ...
  • Failed to install 'cordova-plugin-firebase': CordovaError: Uh oh
    I had follow these steps to install an configure firebase to my cordova project for cloud messaging. https://medium.com/@felipepucinelli/how...
  • Vue3 :style backgroundImage not working with require
    I'm trying to migrate a Vue 2 project to Vue 3. In Vue 2 I used v-bind style as follow: In Vue 3 this doesn't work... I tried a...

Categories

  • Ajax (26)
  • Bootstrap (30)
  • DBMS (42)
  • HTML (12)
  • HTML5 (45)
  • JavaScript (10)
  • Jquery (34)
  • Jquery UI (2)
  • JqueryUI (32)
  • Laravel (1017)
  • Laravel Tutorials (23)
  • Laravel-Question (6)
  • Magento (9)
  • Magento 2 (95)
  • MariaDB (1)
  • MySql Tutorial (2)
  • PHP-Interview-Questions (3)
  • Php Question (13)
  • Python (36)
  • RDBMS (13)
  • SQL Tutorial (79)
  • Vue.js Tutorial (68)
  • Wordpress (150)
  • Wordpress Theme (3)
  • codeigniter (108)
  • oops (4)
  • php (853)

Social Media Links

  • Follow on Twitter
  • Like on Facebook
  • Subscribe on Youtube
  • Follow on Instagram

Pages

  • Home
  • Contact Us
  • Privacy Policy
  • About us

Blog Archive

  • September (100)
  • August (50)
  • July (56)
  • June (46)
  • May (59)
  • April (50)
  • March (60)
  • February (42)
  • January (53)
  • December (58)
  • November (61)
  • October (39)
  • September (36)
  • August (36)
  • July (34)
  • June (34)
  • May (36)
  • April (29)
  • March (82)
  • February (1)
  • January (8)
  • December (14)
  • November (41)
  • October (13)
  • September (5)
  • August (48)
  • July (9)
  • June (6)
  • May (119)
  • April (259)
  • March (122)
  • February (368)
  • January (33)
  • October (2)
  • July (11)
  • June (29)
  • May (25)
  • April (168)
  • March (93)
  • February (60)
  • January (28)
  • December (195)
  • November (24)
  • October (40)
  • September (55)
  • August (6)
  • July (48)
  • May (2)
  • January (2)
  • July (6)
  • June (6)
  • February (17)
  • January (69)
  • December (122)
  • November (56)
  • October (92)
  • September (76)
  • August (6)

  • Failed to install 'cordova-plugin-firebase': CordovaError: Uh oh - 9/21/2024
  • pyspark XPath Query Returns Lists Omitting Missing Values Instead of Including None - 9/20/2024
  • SQL REPL from within Python/Sqlalchemy/Psychopg2 - 9/20/2024
  • MySql Explain with Tobias Petry - 9/20/2024
  • How to combine information from different devices into one common abstract virtual disk? [closed] - 9/20/2024

Laravel News

  • Validate Controller Requests with the Laravel Data Package - 5/19/2025
  • Deployer - 5/18/2025
  • Transform JSON into Typed Collections with Laravel's AsCollection::of() - 5/18/2025
  • Auto-translate Application Strings with Laratext - 5/16/2025
  • Simplify Factory Associations with Laravel's UseFactory Attribute - 5/13/2025

Copyright © 2025 CoderFunda | Powered by Blogger
Design by Coderfunda | Blogger Theme by Coderfunda | Distributed By Coderfunda