在Laravel 10中使用命令行删除控制器的方法是:

huangapple go评论85阅读模式
英文:

How to delete controller in Laravel 10 using command line

问题

我已使用命令 php artisan make:controller AuthLoginRegisterClass 在 Laravel 中创建了控制器。

现在,我想要仅使用命令行删除它,你能帮我吗?

谢谢

英文:

I have created controller in laravel using command php artisan make:controller AuthLoginRegisterClass

Now, I want to delete it using only command line, can you please help me ?

Thank you

答案1

得分: 1

  1. 你可以利用 shell 脚本的力量在终端中删除文件。我创建了一个脚本来删除控制器文件夹中的文件。它会在 `Controllers` 文件夹中查找文件,一旦找到就会删除它。以下是脚本:
  2. ##### delete.sh
  3. ```bash
  4. #!/bin/bash
  5. FILE_NAME=$1
  6. # 在 Controllers 目录中查找文件
  7. FILE_PATH=$(find ./app/Http/Controllers -name "$FILE_NAME" -type f -print -quit)
  8. # 检查文件是否存在
  9. if [ -n "$FILE_PATH" ]; then
  10. # 删除文件
  11. rm "$FILE_PATH"
  12. echo "文件删除成功。"
  13. else
  14. echo "文件未找到。"
  15. fi

你可以为这个文件赋予适当的权限,即 可执行,使用命令 chmod +x delete.sh,然后使用以下命令运行:

./delete.sh FooBarController.sh

希望这能帮助解决问题。

编辑:

你可以使用 php artisan make:command FooCommand 创建一个命令,并在 FooCommand.php 文件中添加以下代码:

  1. protected $signature = 'controller:delete {name : 要删除的控制器文件的名称}';
  2. protected $description = '删除控制器文件';
  3. public function handle()
  4. {
  5. $controllerName = $this->argument('name');
  6. $controllerPath = app_path("Http/Controllers/{$controllerName}.php");
  7. if (File::exists($controllerPath)) {
  8. File::delete($controllerPath);
  9. $this->info("控制器文件 '{$controllerName}' 删除成功。");
  10. } else {
  11. $this->error("控制器文件 '{$controllerName}' 未找到。");
  12. }
  13. }

你的文件将看起来像这样:

  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\File;
  5. class DeleteControllerFile extends Command
  6. {
  7. protected $signature = 'controller:delete {name : 要删除的控制器文件的名称}';
  8. protected $description = '删除控制器文件';
  9. public function handle()
  10. {
  11. $controllerName = $this->argument('name');
  12. $controllerPath = app_path("Http/Controllers/{$controllerName}.php");
  13. if (File::exists($controllerPath)) {
  14. File::delete($controllerPath);
  15. $this->info("控制器文件 '{$controllerName}' 删除成功。");
  16. } else {
  17. $this->error("控制器文件 '{$controllerName}' 未找到。");
  18. }
  19. }
  20. }

现在你也可以使用 php artisan controller:delete foobarController 通过 php artisan 命令删除文件了。

  1. <details>
  2. <summary>英文:</summary>
  3. You can utilize the power of shell script to delete the file using terminal. I created a script to delete the file in controllers folder. It will search for the file inside the `Controllers` folder and delete it once it finds it. Here&#39;s the script:
  4. ##### delete.sh
  5. #!/bin/bash
  6. FILE_NAME=$1
  7. # Find the file in the Controllers directory
  8. FILE_PATH=$(find ./app/Http/Controllers -name &quot;$FILE_NAME&quot; -type f -print -quit)
  9. # Check if the file exists
  10. if [ -n &quot;$FILE_PATH&quot; ]; then
  11. # Remove the file
  12. rm &quot;$FILE_PATH&quot;
  13. echo &quot;File deleted successfully.&quot;
  14. else
  15. echo &quot;File not found.&quot;
  16. fi
  17. You can give this file appropriate permissions i.e. `executable` with `chmod +x delete.sh` and use it with the following command:
  18. `./delete.sh FooBarController.sh`
  19. Hope this helps in resolving the issue.
  20. Edit:
  21. You can create a command with `php artisan make:command FooCommand` and add the following code in the FooCommand.php file:
  22. protected $signature = &#39;controller:delete {name : The name of the controller file to be deleted}&#39;;
  23. protected $description = &#39;Delete a controller file&#39;;
  24. public function handle()
  25. {
  26. $controllerName = $this-&gt;argument(&#39;name&#39;);
  27. $controllerPath = app_path(&quot;Http/Controllers/{$controllerName}.php&quot;);
  28. if (File::exists($controllerPath)) {
  29. File::delete($controllerPath);
  30. $this-&gt;info(&quot;Controller file &#39;{$controllerName}&#39; deleted successfully.&quot;);
  31. } else {
  32. $this-&gt;error(&quot;Controller file &#39;{$controllerName}&#39; not found.&quot;);
  33. }
  34. }
  35. Your file will look something like this:
  36. &lt;?php
  37. namespace App\Console\Commands;
  38. use Illuminate\Console\Command;
  39. use Illuminate\Support\Facades\File;
  40. class DeleteControllerFile extends Command
  41. {
  42. protected $signature = &#39;controller:delete {name : The name of the controller file to be deleted}&#39;;
  43. protected $description = &#39;Delete a controller file&#39;;
  44. public function handle()
  45. {
  46. $controllerName = $this-&gt;argument(&#39;name&#39;);
  47. $controllerPath = app_path(&quot;Http/Controllers/{$controllerName}.php&quot;);
  48. if (File::exists($controllerPath)) {
  49. File::delete($controllerPath);
  50. $this-&gt;info(&quot;Controller file &#39;{$controllerName}&#39; deleted successfully.&quot;);
  51. } else {
  52. $this-&gt;error(&quot;Controller file &#39;{$controllerName}&#39; not found.&quot;);
  53. }
  54. }
  55. }
  56. Now you can also use `php artisan controller:delete foobarController` to delete the file with php artisan command.
  57. </details>
  58. # 答案2
  59. **得分**: 0
  60. 你可以通过运行以下命令来实现:
  61. ```shell
  62. rm app/Http/Controllers/AuthLoginRegisterClass.php
英文:

You can do so by running the following command

  1. rm app/Http/Controllers/AuthLoginRegisterClass.php

答案3

得分: 0

首先,打开命令提示符并导航到您的Laravel项目的根目录 -

  1. cd /path/to/your/Laravel/project

现在,使用'rm'命令,后面跟着控制器的路径,例如 -

  1. rm app/Http/Controllers/ExampleController.php

(如果您的文件位于app/Http/Controllers中)
执行此命令后,控制器文件将从您的项目中永久删除。

英文:

Firstly, open the command prompt and navigate to the root directory of your Laravel project-

  1. cd /path/to/your/Laravel/project

Now, use the 'rm' command followed by the path to the controller like-

  1. rm app/Http/Controllers/ExampleController.php

(if your file is located in the app/Http/Controllers)
after executing this command the controller file will be permanently deleted from your project.

答案4

得分: 0

尽管Hassaan的命令会起作用,但我认为最简单的解决方案是创建一个新的Laravel命令来完成所有工作。无需编写shell脚本。

创建一个新的命令 php artisan make:command DeleteControllerCommand

命令应该类似于这样:

  1. use Illuminate\Console\Command;
  2. use Illuminate\Support\Facades\File;
  3. class DeleteControllerCommand extends Command
  4. {
  5. protected $signature = 'delete:controller {controllerName : 要删除的控制器名称}';
  6. protected $description = '按名称删除一个控制器';
  7. public function handle()
  8. {
  9. $name = $this->argument('controllerName');
  10. $path = app_path("Http/Controllers/{$name}.php");
  11. if (File::exists($path)) {
  12. File::delete($path);
  13. $this->info("{$name} 已删除");
  14. } else {
  15. $this->error("Http/Controllers/ 中不存在 {$name}");
  16. }
  17. }
  18. }

现在如果你运行 php artisan delete:controller AuthLoginRegisterClass,它会检查控制器是否存在,如果存在就删除它。

英文:

Whilst Hassaan's command will work, I think the easiest solution is to just create a new Laravel command that does all the work. No need to write a shell script.

Create a new command php artisan make:command DeleteControllerCommand

The command should look something like this;

  1. use Illuminate\Console\Command;
  2. use Illuminate\Support\Facades\File;
  3. class DeleteControllerCommand extends Command
  4. {
  5. protected $signature = &#39;delete:controller {controllerName : The name of the controller you want to delete}&#39;;
  6. protected $description = &#39;Delete a controller by name&#39;;
  7. public function handle()
  8. {
  9. $name = $this-argument(&#39;controllerName&#39;);
  10. $path = app_path(&quot;Http/Controllers/{$name}.php&quot;);
  11. if (File::exists($path)) {
  12. File::delete($path);
  13. $this-&gt;info(&quot;{$name} deleted&quot;);
  14. } else {
  15. $this-&gt;error(&quot;{$name} does not exist in Http/Controllers/&quot;);
  16. }
  17. }
  18. }

Now if you run php artisan delete:controller AuthLoginRegisterClass it will check if the controller exists and delete it if it does.

huangapple
  • 本文由 发表于 2023年7月6日 18:52:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76628059.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定