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

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

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

你可以利用 shell 脚本的力量在终端中删除文件。我创建了一个脚本来删除控制器文件夹中的文件。它会在 `Controllers` 文件夹中查找文件,一旦找到就会删除它。以下是脚本:

##### delete.sh
```bash
#!/bin/bash

FILE_NAME=$1

# 在 Controllers 目录中查找文件
FILE_PATH=$(find ./app/Http/Controllers -name "$FILE_NAME" -type f -print -quit)

# 检查文件是否存在
if [ -n "$FILE_PATH" ]; then
    # 删除文件
    rm "$FILE_PATH"

    echo "文件删除成功。"
else
    echo "文件未找到。"
fi

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

./delete.sh FooBarController.sh

希望这能帮助解决问题。

编辑:

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

protected $signature = 'controller:delete {name : 要删除的控制器文件的名称}';
        
protected $description = '删除控制器文件';

public function handle()
{
    $controllerName = $this->argument('name');
    $controllerPath = app_path("Http/Controllers/{$controllerName}.php");

    if (File::exists($controllerPath)) {
        File::delete($controllerPath);
        $this->info("控制器文件 '{$controllerName}' 删除成功。");
    } else {
        $this->error("控制器文件 '{$controllerName}' 未找到。");
    }
}

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

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;

class DeleteControllerFile extends Command
{
   protected $signature = 'controller:delete {name : 要删除的控制器文件的名称}';

    protected $description = '删除控制器文件';

    public function handle()
    {
        $controllerName = $this->argument('name');
        $controllerPath = app_path("Http/Controllers/{$controllerName}.php");

        if (File::exists($controllerPath)) {
            File::delete($controllerPath);
            $this->info("控制器文件 '{$controllerName}' 删除成功。");
        } else {
            $this->error("控制器文件 '{$controllerName}' 未找到。");
        }
    }        
}

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


<details>
<summary>英文:</summary>

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: 
##### delete.sh
    #!/bin/bash
    
    FILE_NAME=$1
    
    # Find the file in the Controllers directory
    FILE_PATH=$(find ./app/Http/Controllers -name &quot;$FILE_NAME&quot; -type f -print -quit)
    
    # Check if the file exists
    if [ -n &quot;$FILE_PATH&quot; ]; then
        # Remove the file
        rm &quot;$FILE_PATH&quot;
    
        echo &quot;File deleted successfully.&quot;
    else
        echo &quot;File not found.&quot;
    fi

You can give this file appropriate permissions i.e. `executable` with `chmod +x delete.sh` and use it with the following command: 

`./delete.sh FooBarController.sh`

Hope this helps in resolving the issue.


Edit: 

You can create a command with `php artisan make:command FooCommand` and add the following code in the FooCommand.php file: 

    protected $signature = &#39;controller:delete {name : The name of the controller file to be deleted}&#39;;
        
            protected $description = &#39;Delete a controller file&#39;;
        
            public function handle()
            {
                $controllerName = $this-&gt;argument(&#39;name&#39;);
                $controllerPath = app_path(&quot;Http/Controllers/{$controllerName}.php&quot;);
        
                if (File::exists($controllerPath)) {
                    File::delete($controllerPath);
                    $this-&gt;info(&quot;Controller file &#39;{$controllerName}&#39; deleted successfully.&quot;);
                } else {
                    $this-&gt;error(&quot;Controller file &#39;{$controllerName}&#39; not found.&quot;);
                }
            }

Your file will look something like this: 

    &lt;?php
    
    namespace App\Console\Commands;
    
    use Illuminate\Console\Command;
    use Illuminate\Support\Facades\File;
    
    class DeleteControllerFile extends Command
    {
       protected $signature = &#39;controller:delete {name : The name of the controller file to be deleted}&#39;;
    
        protected $description = &#39;Delete a controller file&#39;;
    
        public function handle()
        {
            $controllerName = $this-&gt;argument(&#39;name&#39;);
            $controllerPath = app_path(&quot;Http/Controllers/{$controllerName}.php&quot;);
    
            if (File::exists($controllerPath)) {
                File::delete($controllerPath);
                $this-&gt;info(&quot;Controller file &#39;{$controllerName}&#39; deleted successfully.&quot;);
            } else {
                $this-&gt;error(&quot;Controller file &#39;{$controllerName}&#39; not found.&quot;);
            }
        }        
    }

Now you can also use `php artisan controller:delete foobarController` to delete the file with php artisan command.

</details>



# 答案2
**得分**: 0

你可以通过运行以下命令来实现:

```shell
rm app/Http/Controllers/AuthLoginRegisterClass.php
英文:

You can do so by running the following command

rm app/Http/Controllers/AuthLoginRegisterClass.php

答案3

得分: 0

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

cd /path/to/your/Laravel/project

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

rm app/Http/Controllers/ExampleController.php

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

英文:

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

cd /path/to/your/Laravel/project

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

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

命令应该类似于这样:

use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;

class DeleteControllerCommand extends Command
{
    protected $signature = 'delete:controller {controllerName : 要删除的控制器名称}';
    protected $description = '按名称删除一个控制器';

    public function handle()
    {
        $name = $this->argument('controllerName');
        $path = app_path("Http/Controllers/{$name}.php");

        if (File::exists($path)) {
            File::delete($path);
            $this->info("{$name} 已删除");
        } else {
            $this->error("Http/Controllers/ 中不存在 {$name}");
        }
    }
}

现在如果你运行 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;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;

class DeleteControllerCommand extends Command
{
    protected $signature = &#39;delete:controller {controllerName : The name of the controller you want to delete}&#39;;

    protected $description = &#39;Delete a controller by name&#39;;

    public function handle()
    {
        $name = $this-argument(&#39;controllerName&#39;);
        $path = app_path(&quot;Http/Controllers/{$name}.php&quot;);

        if (File::exists($path)) {
            File::delete($path);
            $this-&gt;info(&quot;{$name} deleted&quot;);
        } else {
            $this-&gt;error(&quot;{$name} does not exist in Http/Controllers/&quot;);
        }
    }
}

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:

确定