paranoid
paranoid

Reputation: 7105

Run composer dump-autoload from controller in laravel 5

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.

Upvotes: 5

Views: 28487

Answers (5)

Raheel Hasan
Raheel Hasan

Reputation: 6023

You could create a laravel command that uses Symfony\Component\Process\Process such as:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;

class ComposerDumpAutoloadCommand extends Command
{
    protected $name = 'composer:dump-autoload';
    protected $description = 'Run composer dump-autoload';

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

    private function dump_autoload()
    {
        $process = new Process(['composer', 'dump-autoload', '-o']);
        $process->setTimeout(null);

        try {
            $process->mustRun();
            $this->info($process->getOutput());
        } catch (ProcessFailedException $e) {
            $this->error($e->getMessage());
        }
    }    
}

Upvotes: 0

pankaj
pankaj

Reputation: 1906

you can run this for better result. it will give your result like command line.

 return  "<pre>". shell_exec ('composer dump-autoload')."</pre>"; 

Upvotes: -2

Marek Skiba
Marek Skiba

Reputation: 2184

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:

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

    $this->composer = $composer;
}

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

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:

<?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();
    }
}

Upvotes: 12

codedge
codedge

Reputation: 5164

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

Currently there is no way to call composer commands in a proper way from Artisan - not even with creating your own Artisan command with 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.

Upvotes: 12

Circleshair
Circleshair

Reputation: 146

Try this

system('composer dump-autoload');

Upvotes: 8

Related Questions