开发者

Symfony2 script using php shell

How can I start a Symfony2 script using the php shell? I can't run the Controller file directly by using the command:

php FController.php

The path of the controller is

domain.com/web/app_dev.php/fcontroller

Do I h开发者_如何学Pythonave to use the Symfony2 console to run this script?


As has already been said you need to create a console command. Create a directory called 'Command' in one of your bundles (the bundle needs to be registered in AppKernel.php. Then create a class in this directory, it will be automatically found by symfony when you run app/console.

Here is a quick example:

<?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');
    }
}

You can then run the command with:

$> app/console foo:bar-cmd

And pass in options like:

$> app/console foo:bar-cmd  --baz


A console command is the correct thing to use - its purpose is to be your gateway into your application from the command line. If you use the console cookbook chapter, setting up a console command is pretty easy:

http://symfony.com/doc/2.0/cookbook/console.html

You could technically create a PHP script that looks much like your front controller, except that you've modified the Request object to "fake" the web path (e.g. /foo/bar) to execute your controller, but a console command is really the right just for this.

Good luck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜