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

par*_*oid 3 php laravel composer-php laravel-5 laravel-5.2

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

cod*_*dge 8

工匠不是包装纸composer.Composer本身带来了composer控制自身的命令.

目前没有办法从Artisan composer适当的方式调用命令- 甚至没有创建自己的Artisan命令php artisan make:console CommandName.

除非你不想使用PHP,exec或者system我强烈建议不要使用PHP,否则你最好独立运行composer dump-autoload.


小智 6

尝试这个

system('composer dump-autoload');
Run Code Online (Sandbox Code Playgroud)


Mar*_*iba 6

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

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

    $this->composer = $composer;
}
Run Code Online (Sandbox Code Playgroud)

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

public function handle()
{
    $this->composer->dumpAutoloads();
}
Run Code Online (Sandbox Code Playgroud)

检查类MigrateMakeCommand中是否vendor/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();
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 对于使用laravel 5.5及更高版本的用户。更改使用以使用Illuminate \ Support \ Composer; (2认同)