Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 script using php shell

Tags:

symfony

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 have to use the Symfony2 console to run this script?

like image 399
Daniel Avatar asked May 16 '26 10:05

Daniel


1 Answers

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
like image 128
markymark Avatar answered May 19 '26 01:05

markymark



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!