6ber6ou's avatar

Run composer command in a controller

Hi all.

How can I run a composer command in a Laravel controller ? When a user submit a form I want to fire off this commands :

cd code
composer create-project laravel/laravel my-project
0 likes
7 replies
6ber6ou's avatar

This command allow us to fire off a artisan command. What I want is to fire off a composer command.

MatteoOreficeIT's avatar

Hi

The Laravel / Symfony way we have found is :


        $phpFinder = new PhpExecutableFinder();
         
        if($phpBin = $phpFinder->find()) {
            // Removing composer dev packages'
            $cmdLine = "$phpBin ".base_path('lib/composer/composer.phar').' install --no-dev';
            $process = new Process(str_replace('\\','/',$cmdLine),base_path($this->getDistPath()));
            $process->setTimeout(0);
            $process->mustRun(function($type,$line){
                // get output if needed
            });
        }

For clarity we distribute the composer.phar in our application so we don't depend on a global composer install, the path is reachable within project base folder

cvhainb's avatar

Hello bro,

I think your solution is best however I can't run it. I don't know what this is: base_path($this->getDistPath())

This is my code $cmdLine = "$phpBin ".base_path('composer.phar').' update --no-progress --no-scripts'; $process = new Process([str_replace('\','/',$cmdLine), base_path()]); $process->setTimeout(0); $process->mustRun();

but it's error: Error Output:

The filename, directory name, or volume label syntax is incorrect.

Please advise.

Best regards,

Matt

Please or to participate in this conversation.