Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Silex Application in Command Line

Tags:

php

symfony

silex

I'd like to run a Silex Application like this in Command Line:

$app = new Silex\Application(); 

$app->get('/hello/{name}', function($name) use($app) { 
  return 'Hello '.$app->escape($name); 
}); 

$app->run(); 

I think for that purpose, i'd have to pass Symfony's Request Object as first parameter to the run method, but i've got no idea, where to set the Url-Path to make it work. Any Ideas? Or is there a better way to do this?

like image 457
kertal Avatar asked Sep 04 '12 15:09

kertal


2 Answers

Here's a simple way to do it:

list($_, $method, $path) = $argv;
$request = Request::create($path, $method);
$app->run($request);

And then on the command line:

$ php console.php GET /
like image 54
igorw Avatar answered Nov 15 '22 19:11

igorw


If you want to use silex in a command line, you need to use the Console Component, here a tutorial for silex: http://beryllium.ca/?p=481

Then you are able to call a twig (symfony) service, and to forward an action !

http://symfony.com/doc/current/cookbook/console/console_command.html#getting-services-from-the-service-container

like image 5
Sybio Avatar answered Nov 15 '22 20:11

Sybio