1

I want to build a private ecosystem with multiple reusable bundles, similar to the Sonata project. This is my first time so I followed Symfony2 - creating own vendor bundle - project and git strategy and set up a simple bundle named PUIEconomyBundle with a DefaultController. I imported the bundle into an example project from my Git repo using composer.json.

Now i'm running into a 404 No route found for "GET /test". It's important to have annotated routes to keep an overview. How do I introduce working annotated routing into my controllers? The debug:router does not mention the route from this bundle, although the profiler says the PUIEconomyBundle is enabled.

DefaultController:

class DefaultController extends Controller
{
    /**
     * @Route("/test", name="homepage")
     * @param Request $request
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function indexAction(Request $request)
    {
        dump('Hello!');die;
    }
}

Extension:

public function load(array $configs, ContainerBuilder $container)
{
    $configuration = new Configuration();
    //$config = $this->processConfiguration($configuration, $configs);

    $fileLocator = new FileLocator(__DIR__.'/../Resources/config');
    $loader = new Loader\YamlFileLoader($container, $fileLocator);
    $loader->load('services.yml');
}

Services.yml:

services:
    pui_economy.routing_loader:
        class: Company\PUI\EconomyBundle\Service\RoutingLoader
        tags:
            - { name: routing.loader }

RoutingLoader:

class RoutingLoader extends Loader
{
    public function load($resource, $type = null)
    {
        $collection = new RouteCollection();

        $resource = '@PUIEconomyBundle/Resources/config/routing.yml';
        $type = 'yaml';

        $importedRoutes = $this->import($resource, $type);

        $collection->addCollection($importedRoutes);

        return $collection;
    }

    public function supports($resource, $type = null)
    {
        return 'advanced_extra' === $type; // ??
    }
}

Routing.yml:

pui_economy:
    resource: "@PUIEconomyBundle/Controller"
        type: annotation

Thank you

Community
  • 1
  • 1

3 Answers3

0

It seems that you forget to add this:

app_extra:
    resource: .
    type: extra

in app/config/routing.yml.

See Using the custom Loader.

Rendy Eko Prastiyo
  • 1,068
  • 7
  • 16
  • I just tried to implement the 'extra' custom loader with the route to `/extra/{parameter}` (also added the method to my DefaultController). Again a 404. I have the idea that the routingloader isn't loaded by the DelegatingLoader, but `php bin/console debug:container` shows my RoutingLoader.. Does Symfony also recognize the `Resources/config/routing.yml` path in every **vendor** bundle or do I need to add something in PUIEconomyBundle.php or PUIEconomyExtension.php? – Marnix Bent Feb 12 '17 at 17:10
  • Router will load the routes you have registered in your custom loader. You have PUIEconomyBundle/Resources/config/routing.yml (which points to @PUIEconomyBundle/Controller) and you registered it in your custom loader. In indexAction of your DefaultController class, you mention @Route /test, means you can access that action by pointing your browser to something like http://host/test. – Rendy Eko Prastiyo Feb 13 '17 at 03:06
0

Why are u using a custom routing loader? This is a pretty adavanced topic which is not neccessary to simply bind a controller on a route via annotations.

You can find a working example for the @Route annotation here: https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing.html

You should also remove the die() statement. Symfony mabey wouldn't give you a response if you kill the request in this way.

flxPeters
  • 1,476
  • 12
  • 21
  • Hi el_wichtel, Im trying to get annotated routes working but it gives a 404 on everything I try. With or without custom routing loaders.. – Marnix Bent Feb 15 '17 at 19:37
  • Can you see the route via the bin/console debug:router command? – flxPeters Feb 15 '17 at 20:42
  • Nope, no routes from my bundle there, but I do see a route from the default AppBundle. The profiler says PUIEconomyBundle is enabled though – Marnix Bent Feb 15 '17 at 22:29
0

You don't need custom route loader to load your bundle annotated routes.

I was facing similar issue. All we need is to put this configuration in application where we want to load bundle

config/routes.yaml

my_cool_bundle_routes:
  # loads routes from the given routing file stored in some bundle
  resource: '@XyzAuthBundle/Controller/'
  type:     annotation

and that's all :)

my_cool_bundle_routes naming is not significant. It just have to be unique.

R Sun
  • 1,353
  • 14
  • 17