How do you manually create Controllers in Symfony2 without the use of the command line?

1.4k views Asked by At

I am unable to create Controllers through the Command line. Does anyone know how can I manually create controllers for my Symfony2 project?

What files I need to update?

1

There are 1 answers

2
Michael Sivolobov On

You should create file YourControllerNameController.php at src/YourBundle/Controller folder. And put class with name YourControllerNameController into this file. Also be assured that you wrote right namespace according to PSR-0.

If you create controller as a service you can define it as a service and have no problem.

If you want create standard controller that are not registered at service container you could extend your class from Symfony\Bundle\FrameworkBundle\Controller\Controller

In both cases you need also define route for your actions. The easiest way would be to define one route with annotation type. It will generate routes automatically based on your annotations related to actions:

your_route_name:
    resource: "@YourBundle/Controller"
    type:     annotation

It will scan your src/YourBundle/Controller folder and will generate new route for any method in any class in any file that will have annotation @Route:

/**
 * @Route("/your/path", name="you_can_specify_name_here_but_it_is_optional")
 */
public function yourAction()
{
    // Your code here
}