3

I would like to execute the following command : composer update in my controller in laravel, however this one does not work. On the other hand the command composer info works perfectly.

When I execute composer update in a command prompt all my dependencies are correctly updated in the laravel vendor file but when I try to execute composer update in my controller nothing happens.

here is my code :

$data['output'] = shell_exec( 'cd '. base_path() .' && composer update' );
dd($data);

and here is the result :

array:1 [▼
  "output" => null
]

Could you help me to understand why composer update does not work in a controller ? I would like to update my dependencies in a controller without the command prompt.

Thank you.

HIBO
  • 31
  • 4
  • 2
    "Does not work" is not a good description. What does not work? Any errors? – common sense Jan 26 '18 at 21:48
  • Is shell_exec returning null? – jgetner Jan 26 '18 at 21:54
  • yes sorry, for my poor description,shell_exec returns "output" => null – HIBO Jan 26 '18 at 22:33
  • 3
    the user in your CLI differs from the user that is executing your code over your webserver most likely. Also this could be an absolute catastrophe of an idea under the *right* circumstances... – Ohgodwhy Jan 26 '18 at 22:52
  • OK thanks, I wanted to make an interface so that the website admin can easily install packages. He would have just had to give that kind of informations : SSH link -> `git@bitbucket.org:xxxxx/xxxxxx.git` and Install version -> `"xxxxx/xxxxxx": "0.1.*"`. And The web application would have to update the _composer.json_ and _conf/app.php_ files by itself and then execute the command `composer update`. however, it is may be impossible to run the "composer update" command in a controller (as a precaution). What do you think? – HIBO Jan 26 '18 at 23:47
  • @Ohgodwhy your nickname is an appropriate response to the doing of `composer update` via a route/controller, lol. – Shafiq al-Shaar Jan 26 '18 at 23:56

1 Answers1

2

From the php docs for shell_exec():

The output from the executed command or NULL if an error occurred or the command produces no output.

Sounds like you'd be better off using passthru() instead as that will give you the erroneous output.

Speaking of erroneous, this is idea sounds like a recipe for unmitigated disaster.

jahsome
  • 881
  • 8
  • 18
  • Thank you for your answer. I know this is not a very good idea (if you know another way to install a package from git, I'm interested thank you ). However with your answer I get the same result, I think I will try to install my dependencies in 3 steps, **the first** will automatically configure my file _composer.json_ and _config/app.php_, **the second** is to run manually the command `composer update` and finally **the third** will automatically replace the _composer.lock_ file and automatically execute the command `composer install --no-dev`. A bit like here [link](https://lc.cx/gzAF). – HIBO Jan 27 '18 at 10:10
  • You can also try to pipe the output to a file (e.g. `exec('composer update > output.txt')`) and try to find that file. That should (hopefully) at least allow you to see the composer output. I can't think of a good way to allow users to install composer packages. I would highly recommend at the very least you do an install on the individual package and _never_ run composer update in a production environment. – jahsome Jan 29 '18 at 18:17