Skip to content

Instantly share code, notes, and snippets.

@miftahafina
Last active February 3, 2025 19:44
Show Gist options
  • Select an option

  • Save miftahafina/292deed1265f2e404e5cd2229ef3ed78 to your computer and use it in GitHub Desktop.

Select an option

Save miftahafina/292deed1265f2e404e5cd2229ef3ed78 to your computer and use it in GitHub Desktop.
Remove laravel controller with artisan command
<?php
/**
* Copy and paste the code below into your routes/console.php file.
* You can add more command such as 'remove:model', 'remove:middleware', etc if you want,
* just modify the $file_location variable.
*/
Artisan::command('remove:controller {name : Name of the controller}', function ($name) {
// File location
$file_location = base_path() . '/app/Http/Controllers/' . $name . '.php';
// Check if exist
if (file_exists($file_location)) {
exec('rm ' . $file_location);
$this->info($name.' has been deleted!');
} else {
$this->error('Cannot delete ' . $name . ', file not found.');
}
})->describe('Remove spesific controller');
@iPaul-SE

Copy link
Copy Markdown

Code is 4 years old but still worked a treat despite the various versions of Laravel that have come since.
Given all the make commands, I'm surprised there aren't built in remove commands...

@Emmanou1998

Copy link
Copy Markdown

j'ai un beug sur laravel impossible d'accéder sur mon backend voilà ce qu'il m'affiche comme erreur "include(C:\laragon\www\backup4\project\vendor\composer/../../app/Http/Controllers/Controller.php): failed to open stream: No such file or directory" car j'ai fait une faute de frappe en voulant créé un controllers, comment ressoudre se beug

@elhakimdev

Copy link
Copy Markdown

@elhakimdev are you using linux?

No. Im using windiws

@rroossyyiidd

Copy link
Copy Markdown

@zera272 tujuannya biar anu... pas typo generate controllernya. Rare banget sih, emang. 😆

wkwk sangar

@abdasis

abdasis commented Mar 8, 2022

Copy link
Copy Markdown

Tambah hapus model om sekalian 😍

@miftahafina

miftahafina commented Mar 9, 2022

Copy link
Copy Markdown
Author

Tambah hapus model om sekalian 😍

Bisa, cukup ganti pathnya aja di variabel $file_location

@eraser16

eraser16 commented Dec 7, 2022

Copy link
Copy Markdown

Hi. What should I change $file_location to?

@zalaudekr

Copy link
Copy Markdown

@Luckiees you have to change the command('remove:controller and path /app/Http/Controllers/ if you want to remove something else than a controller. It's a good example how to create a command for the console but dangerous because as written before it removes your code from the disk and don't reflect on routes and other usage of your controller.

@eraser16

eraser16 commented Jan 8, 2023

Copy link
Copy Markdown

@zalaudekr Thank you so much😄

@milkiainunistaufan

Copy link
Copy Markdown

Good

@TatiSamuel

Copy link
Copy Markdown

@miftahafina
miftahafina Thank you.

@ahjohaa33

Copy link
Copy Markdown

@miftahafina could it be possible this way to remove the entire controller folder?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment