Laravel

Symfony Process with Laravel

Max Hutschenreiter -

Are you using ‘exec’, ‘passthru’, ‘shell_exec’ and ‘system’ PHP functions in your Laravel project?

Taking care of security risks by yourself, as well as expecting differences per operating system which project runs on?

Seems like it’s time to review Symfony Component ‘Process’, which allows you to handle things outside of the project way easier, with high input-output control, and it’s escaping arguments to prevent security issues.

It comes with features like:

  • real-time Process Output,
  • Exception handling
  • Running Processes Asynchronously,
  • Setting Environment Variables for Processes,
  • Streaming to the Standard Input of a Process,
  • Using a Prepared Command Line,
  • wait() function,
  • Process Timeout,
  • Process Signals
Which gives you complete control over the process you are running on the server.

I found it’s great usage while handling the feature where I had to run a specific python file, which required passed arguments:
use Symfony\Component\Process\Process; 
use Symfony\Component\Process\Exception\ProcessFailedException; 
 
 
$process = new Process(['python','/path/to/your_script.py',$arg(optional)]); 
$process->run(); // executes after the command finishes 
 
if (!$process->isSuccessful()) { 
   throw new ProcessFailedException($process); 
} 
echo $process->getOutput();
Official documentation for this component: https://symfony.com/doc/current/components/process.html

Then, it’s time to go for it:
composer require symfony/process
But before you do, also review Terminal, which is an elegant wrapper around the Symfony Process Component:
https://github.com/TitasGailius/terminal

Tags: Laravel process · Symfony proccess

Want products news and updates?

Sign up for our newsletter to stay up to date.

We care about the protection of your data. Read our Privacy Policy.

Impressions from our Team

  • Happy birthday 🎁🎈🎂 Filip - #

  • Another day another #mandarinacakeshop 🎂 😀 - #

  • Happy Birthday Ognjen! And marry Christmas to all other 🎄#notacakeshop - #

  • #Office #Garden - #

  • #workhard - #

  • #belgrade #skyline - #

  • #happybirthday Phil :) - #

  • #happybirthday Stefan 🥂 - #

  • #happybirthday Lidija 🍾 - #

  • Say hi 👋 to our newest team member ☕️ - #

  • #bithday #cake 😻 - #

  • #stayathome #homeoffice #42coders - #

  • #stayathome #homeoffice #42coders #starwars :) - #

  • #stayathome #homeoffice #42coders - #

  • We had a really nice time with #laracononline #laravel - #

  • Happy Birthday 🎂 Miloš - #

  • Happy Birthday 🎂Nikola - #

  • #42coders #christmas #dinner what a nice evening :) - #

  • Happy Birthday 🎂 Ognjen - #

  • Wish you all a merry Christmas 🎄🎁 - #

See more!

© 2024 42coders All rights reserved.