在 laravel 5 中从控制器运行 composer dump-autoload

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37238547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:48:26  来源:igfitidea点击:

Run composer dump-autoload from controller in laravel 5

phplaravellaravel-5composer-phplaravel-5.2

提问by paranoid

I want to run composer dump-autoloadwithout shell command in controller.
In laravel 4 I use Artisan::call('dump-autoload');
but in laravel 5 this command not work.

我想在composer dump-autoload没有 shell 命令的情况下在控制器中运行。
在 laravel 4 我使用Artisan::call('dump-autoload');
但在 laravel 5 这个命令不起作用。

回答by Marek Skiba

There is no Artisan::call('dump-autoload');command in >= Laravel 5.0, but if you still want to use this command and don't want use solutions with execor system, you need create your own command by: php artisan make:console DumpAutoloadfor Laravel version > 5.3 (You need add new command to $commandsarray in app/Console/Kernel.php). Then you need inject Composer class to you new command construction:

Artisan::call('dump-autoload');>= Laravel 5.0 中没有命令,但是如果您仍然想使用此命令并且不想使用exec或解决方案system,则需要通过以下方式创建自己的命令:php artisan make:console DumpAutoload对于 Laravel 版本 > 5.3(您需要将新命令添加到$commands数组在app/Console/Kernel.php)。然后你需要将 Composer 类注入到你的新命令构造中:

public function __construct(Composer $composer)
{
    parent::__construct();

    $this->composer = $composer;
}

and then you can run dumpAutoloads()in handle()method:

然后你可以dumpAutoloads()handle()方法中运行:

public function handle()
{
    $this->composer->dumpAutoloads();
}

Check class MigrateMakeCommandin vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/MigrateMakeCommand.phpthere is an example command that use it. Here you have my command:

检查类MigrateMakeCommandvendor/laravel/framework/src/Illuminate/Database/Console/Migrations/MigrateMakeCommand.php有一个使用它的示例命令。你有我的命令:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Foundation\Composer;

class DumpAutoload extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'dump-autoload';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Regenerate framework autoload files';

    /**
     * The Composer instance.
     *
     * @var \Illuminate\Foundation\Composer
     */
    protected $composer;

    /**
     * Create a new command instance.
     *
     * @param Composer $composer
     * @return void
     */
    public function __construct(Composer $composer)
    {
        parent::__construct();

        $this->composer = $composer;
    }

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function handle()
    {
        $this->composer->dumpAutoloads();
        $this->composer->dumpOptimized();
    }
}

回答by codedge

Artisan is not wrapper for composer. Composer itself brings the composercommand to control itself.

Artisan 不是composer. Composer 本身带来了composer控制自己的命令。

Currently there is no way to call composercommands in a properway from Artisan - not even with creating your own Artisan command with php artisan make:console CommandName.

目前没有办法从 Artisancomposer正确的方式调用命令- 即使使用php artisan make:console CommandName.

Unless you don't want to use PHPs execor system, which I highly do not recommend, you better run composer dump-autoloadon its own.

除非您不想使用 PHPexecsystem,我强烈不建议您使用 PHP或,否则您最好composer dump-autoload自行运行。

回答by Circleshair

Try this

尝试这个

system('composer dump-autoload');