Noor Alam Profile picture
Dev at Monico Technologies Ltd. | Write code with #php #Laravel #vue #js

Aug 14, 2022, 7 tweets

#Laravel: Create custom command to make custom class. The example shows how we create a DataTransferObject (DTO) Class by command

🧵Thread

Above code will generate DTO class for us. You need some changes.

You need to change the stub path (location of your stub file) on getStub()

You need to change the DTO Class savings directory (location of your DTOclasses) on getDefaultNamespace()

Stub File
/stubs/DTO.stub

Notice the {{ class }} syntax in the stub file. When we create a DTO class by "php artisan make:dto UserDTO" command, Laravel GeneratorCommand will replace the {{ class }} syntax with UserDTO in the actual DTO class (UserDTO.php)

Of course you can add custom syntax to you stub file like {{ DummyRequestClass }} and {{ DummyRequestVariable }}

After adding custom syntax to your stub file you have to tell your command to replace those syntax with certain Words. you can do that by overriding buildClass()

UserDTO class (/DTOs/UserDTO.php)

Yellow highlighted words are replaced

Share this Scrolly Tale with your friends.

A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.

Keep scrolling