在命令行中运行Silex应用程序

ker*_*tal 9 php symfony silex

我想在命令行中运行这样的Silex应用程序:

$app = new Silex\Application(); 

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

$app->run(); 
Run Code Online (Sandbox Code Playgroud)

我想为此目的,我必须将Symfony的Request Object 作为第一个参数传递给run方法,但我不知道,在哪里设置Url-Path以使其工作.有任何想法吗?或者有更好的方法吗?

igo*_*orw 21

这是一个简单的方法:

list($_, $method, $path) = $argv;
$request = Request::create($path, $method);
$app->run($request);
Run Code Online (Sandbox Code Playgroud)

然后在命令行上:

$ php console.php GET /
Run Code Online (Sandbox Code Playgroud)


Syb*_*bio 5

如果要在命令行中使用silex,则需要使用控制台组件,此处为silex教程:http://beryllium.ca/?p = 481

然后你就可以调用一个twig(symfony)服务,并转发一个动作!

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

  • Webarchive版本的教程:https://web.archive.org/web/20121108160050/http://beryllium.ca/?p = 481 (3认同)