Waiting for Dev...
Waiting for Dev...

Reputation: 13037

Run Symfony2 console command from the web browser

I have to work with a production server that sucks. I can use Symfony2 required PHP version through apache, but not in the CLI, which uses an older PHP version.

So my question is... is there any way I could run console commands from the web browser? So, moving the console file to the server root directory and doing some changes... Later, for security reasons, of course, this file would be stored outside the root.

Upvotes: 4

Views: 8876

Answers (6)

auipga
auipga

Reputation: 329

A more simple solution for running a command:

<pre>
<?php

$output = $return_var= null;
$command = __DIR__.'/../app/console -v cache:clear -e=prod';
echo "Running: $command\n";
exec($command, $output, $return_var);

if (!empty($output) && is_array($output)) {
    echo "Output:\n";
    foreach ($output as $line) {
        echo $line."\n";
    }
} else {
    echo 'Cache could not be cleared: '.var_export($return_var, true);
}

Adapted from https://github.com/jerome-fix/symfony-cmf-standard-with-sonata/blob/master/web/reload-fixtures.php

Upvotes: 1

Stephanos
Stephanos

Reputation: 57

I recently tried to install the ConsoleBundle mentoined above but composer said it's abandoned. Instead of that use this: https://github.com/CoreSphere/ConsoleBundle

Upvotes: 3

Roberto Rizzi
Roberto Rizzi

Reputation: 1533

I put up a customised solution to this issue.

After a whole morning of pain and a desperate trying to find a good solution, below the answer to your question.

1) Create a file .bat into the web dir like here:

your_symfony_dir/www/web/console.bat

This is the file console.bat content:

set cmd=%~1
cd ..
php app/console %cmd%

2) Create a file .php into the web dir like here:

your_symfony_dir/www/web/console.php

This is the file console.php content:

<?php
if (!preg_match('/^192\.168\.(1)\.[0-9]+$/', $_SERVER['REMOTE_ADDR'])) {
    die("Access Denied!");
}
?>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Console</title>
    </head>
    <body style='font-family: monospace;'>
        <h1>Console</h1>
        <form method='POST'>
            <fieldset>
                <legend>Comandi base</legend>               
                <p><button name='pre_cmd[]' value='cache:clear'>Clear Cache</button></p>
                <p><button name='pre_cmd[]' value='assets:install'>Install Assets</button></p>
            </fieldset>
            <fieldset>
                <legend>Comando personalizzato</legend>             
                <p>php app/console <input type='text' name='cmd' value='' /><input type='submit' value='Esegui' name='submit' /> <input type='reset' value='Annulla' /></p>
            </fieldset>
        </form>
        <?php
        if ($_POST) {
            $cmd = $_POST['cmd'] ? $_POST['cmd'] : (is_array($_POST['pre_cmd']) ? $_POST['pre_cmd'][0] : false);
            if ($cmd) {
                echo "<div style='color:white;background:black;padding:10px'>";
                echo "<pre>";
                $output = array();
                exec($_SERVER['DOCUMENT_ROOT'] . '\console.bat "' . $cmd . '"', $output);
                foreach ($output as $o) {
                    echo htmlspecialchars($o);
                    echo PHP_EOL;
                }
                echo "</pre>";
                echo "</div>";
            } else {
                echo "<p>Comando vuoto</p>";
            }
        }
        ?>
    </body>
</html>

Where summary, i get the post values (the command-line input e.g. "cache:clear") and i execut it into the .bat file through the command:

exec($_SERVER['DOCUMENT_ROOT'] . '\console.bat "' . $cmd . '"', $output);

3) When you'll execute the url www.tuodominio.it/console.php you'll see the form where you can input your command (like cache:clear). For my comfort, into the bat file i have already set up the "app/console" part, so i have to digit just "cache:clear" or "assets:install", etc

Upvotes: 0

herrjeh42
herrjeh42

Reputation: 2793

I wanted to clear the cache from the browser, the whole ConsoleBundle (see accepted answer above) was too much for me (and still too nerdy for my users). I figured out that you can easily use this bundle without the user interface it provides.

// install the bundle & run this code, e.g. in a controller
$console = new \CoreSphere\ConsoleBundle\Executer\CommandExecuter($this->container->get('kernel'));
$response = $console->execute("cache:clear --env=prod");
var_dump($response); // returns output and error code etc 

Upvotes: 3

samuel-queniart
samuel-queniart

Reputation: 167

There is a bundle for the console in the browser.

https://github.com/winzou/ConsoleBundle

It is used for shared servers.

Upvotes: 13

hacfi
hacfi

Reputation: 766

Not as far as I know but you should just use the php version which is used by Apache! Either setup an alias in your .bash_profile / .zshrc: alias php='/path/to/php/used/by/apache/bin/php' or always run app/console with the entire path to php /path/to/php/used/by/apache/bin/php app/console

Or if the configuration/php.ini is the problem find out which one is used: php -i | grep php.ini Result: Loaded Configuration File => /etc/php5/cli/php.ini" for example You can use a different config file with the --php-ini or -c parameter: php -c /etc/php5/cgi -i | grep php.ini

Upvotes: 1

Related Questions