在 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
Run composer dump-autoload from controller in laravel 5
提问by paranoid
I want to run composer dump-autoload
without 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 exec
or system
, you need create your own command by: php artisan make:console DumpAutoload
for Laravel version > 5.3 (You need add new command to $commands
array 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 MigrateMakeCommand
in vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/MigrateMakeCommand.php
there is an example command that use it. Here you have my command:
检查类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();
}
}
回答by codedge
Artisan is not wrapper for composer
. Composer itself brings the composer
command to control itself.
Artisan 不是composer
. Composer 本身带来了composer
控制自己的命令。
Currently there is no way to call composer
commands 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 exec
or system
, which I highly do not recommend, you better run composer dump-autoload
on its own.
除非您不想使用 PHPexec
或system
,我强烈不建议您使用 PHP或,否则您最好composer dump-autoload
自行运行。
回答by Circleshair
Try this
尝试这个
system('composer dump-autoload');