sajjad
sajjad

Reputation: 58

how to add a method to all controller by default in laravel?

i want to add new method to each controller that created by artisan command for example if i run "php artisan make:controller BookController" then i want that BookController file has a method named 'details' by default any way?

Upvotes: 1

Views: 845

Answers (3)

Milad pegah
Milad pegah

Reputation: 349

All controllers extends App\Http\Controllers\Controller class. add your method to controller(App\Http\Controllers\Controller.php) and use that in all your controllers.

Upvotes: 2

user8034901
user8034901

Reputation:

Laravel uses "stubs" when creating new models/controllers/migrations etc using php artisan make:....

Run php artisan stub:publish to publish the default stubs into a /stubs/ folder in your project. You can then easily customize those stubs to your liking.

Stub Customization

Upvotes: 3

Vladyslav Shubonkin
Vladyslav Shubonkin

Reputation: 1

I believe you will need to write your own custom make command to achieve it. There is a link I found that might help you - https://medium.com/@imfx/create-make-custom-commands-in-laravel-9fa002da582e.

Basically, you will need to overwrite make:controller and use your own stub.

Upvotes: 0

Related Questions