-1

I am working on a project where I have about 98 controllers. We have decided to migrate this project to Symfony. I have been able to create a custom Bundle called "mdBundle".

It gets registered and I able to access the controller without any issue if I use type: annotation in the routing.yml file.

However because we have so many controllers it will take forever to create the anotations in the actions. Therefore I am trying to come up with a way that my routing.yml can handle any request and execute the controller requested.

Right now my routing.yml looks like this:

mdRoute:
    path: /{_controller}/{_action}/
    defaults: { _controller: mdBundle:_controller:_action }

My Controller(DefaultController.php) is like this:

namespace mdBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class DefaultController extends Controller
{

public function indexAction(Request $request)
{
    return new Response('foo');

}

public function testingAction(Request $request)
{
    return new Response('Bar');

}
}

When I run it I get the following error: LogicException in ControllerResolver.php line 69: Unable to parse the controller name "Default".

My folder structure is a bit different than symfony's default. My Bundle is inside /root/app/mdBundle/ (<-- From here it is the same as Symfony). Anybody has any idea why this is not working.

Or what could be another way to accomplish this without having to annotate every single action in my 98 contollers?

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
zeid10
  • 511
  • 8
  • 28
  • 98 controllers? Not very many. Take the 30 minutes and add them to your route file. Otherwise, search for dynamic routing. – Cerad Mar 17 '16 at 23:32

1 Answers1

2

You can import all routes for a specific controller, or for a whole Controller directory.

This will import all routes defined inside all your controllers from MdBundle.

#app/config/routing.yml
app_mdbundle:
    resource: "@MdBundle/Controller"

This will import all routes defined inside a specific controller inside MdBundle:

#app/config/routing.yml
app_md_bundle:
    resource: "@MdBundle/Controller/YourController"
    type:     annotation

Take a look here: http://symfony.com/doc/current/book/routing.html

You really should define your bundle inside src/ directory. And for this you have two options: let the robots do the job for you ( using: php app/console generate:bundle, when you are inside the root directory [ and if you're using Symfony < v3 ], or $ php bin/console generate:bundle if you're using Symfony >= 3] ), or by creating it manually: inside src/, create new dirs (MdBundle/Controller/YourController1.php, and inside MdBundle you can create the other dirs, like Command,Resources/views, Resources/config, etc). Take a look here: http://symfony.com/doc/current/bundles/SensioGeneratorBundle/commands/generate_bundle.html

And don't use _controller as parameter for that route. http://symfony.com/doc/current/book/controller.html The rule is simple:

mdRoute:
    path: /hello/{name}
    defaults: { _controller: mdBundle:Default:yourAction }

This will match all routes like /hello/*, and it will execute the yourAction action method inside DefaultController inside mdBundle.

LE:

Yes, you can accomplish that too:

# src/MdBundle/Controller/HelloController.php
class HelloController{
    /**
      * @Route("/hello/foo", name="whatever1")
      */
    public function fooAction(){ ... }
}

# src/MdBundle/Controller/Hello2Controller.php
class Hello2Controller{
    /**
      * @Route("/hello2/foo", name="whatever2")
      */
    public function fooAction(){ ... }
}

And if you don't want to write all the routes above the action methods, you can create MdBundle/Resources/config/routing.yml, and here put all your routes:

app_hello:
    path: /hello/foo
    defaults: { _controller: MdBundle:Hello:foo }

app_hello2:
    path: /hello2/foo
    defaults: { _controller: MdBundle:Hello2:foo }

And then import all routes in the main config file:

# app/config/routing.yml
MdBundle:
    resource: "@MdBundle/Resources/config/routing.yml"

Or define, directly, the routes above, in the main app/config/routing.yml file.

Dan Costinel
  • 1,716
  • 1
  • 15
  • 23
  • what if i am not using the Default controller. What i am trying to avoid is to define all the routes in the actions. In other words the controller would be changed dynamically. Example if i go to /hello/foo it will go to hello cotroller,if i have /hello2/foo it will go to hello2 controller(without having defined the route in the foo action). you get it? – zeid10 Mar 17 '16 at 20:08
  • what you mean by LE? sorry. – zeid10 Mar 17 '16 at 20:26