使用php shell的Symfony2脚本

Dan*_*iel 3 symfony

如何使用php shell启动Symfony2脚本?我无法使用以下命令直接运行Controller文件:

php FController.php
Run Code Online (Sandbox Code Playgroud)

控制器的路径是

domain.com/web/app_dev.php/fcontroller
Run Code Online (Sandbox Code Playgroud)

我是否必须使用Symfony2控制台来运行此脚本?

mar*_*ark 6

如前所述,您需要创建一个控制台命令.在你的一个bundle中创建一个名为'Command'的目录(该bundle需要在AppKernel.php中注册.然后在这个目录中创建一个类,当你运行app/console时,symfony会自动找到它.

这是一个简单的例子:

<?php
namespace Acme\FooBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\Command,
    Symfony\Component\Console\Input\InputOption,
    Symfony\Component\Console\Input\InputInterface,
    Symfony\Component\Console\Output\OutputInterface;

class BarCommand extends Command
{

    protected function configure()
    {
        $this
            ->setName('foo:bar-cmd')
            ->setDescription('Test command')
            ->addOption('baz', null, InputOption::VALUE_NONE, 'Test option');
        ;
    }

    /**
     * Execute the command
     * The environment option is automatically handled.
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $output->writeln('Test command');
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以运行以下命令:

$> app/console foo:bar-cmd
Run Code Online (Sandbox Code Playgroud)

并传递以下选项:

$> app/console foo:bar-cmd  --baz
Run Code Online (Sandbox Code Playgroud)