0

Helo guys, im a Symfony noob so please bear with me

i have a simple controller like so

<?php

namespace Sites\AllBundle\Controller;

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

// these import the "@Route" and "@Template" annotations
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;


class SitesController extends Controller
{
    /**
     * @Route("/", name="_all")
     * @Template()
     */
    public function indexAction()
    {       
        return array('name' => 'sssssss');
    }

    /**
     * @Route("/wow",)
     * @Template()
     */
    public function wowAction()
    {       
        return array('wow' => 'sssssss');
    }

}

And my routing.yml for this bundle is like this

_all:
    resource: "@SitesAllBundle/Controller/SitesController.php"
    type:     annotation
    prefix:   /sites

And the routing in app/config/routing.yml is like so

sites_all:
    resource: "@SitesAllBundle/Resources/config/routing.yml"

Simple enough and it works fine......but i dont want add this

@Route("/")

in the comment for every function, and if i remove it, it dosent work.

Is there a way to make the controller/function directly point to that function ?

Thanks guys

1 Answer 1

1

If I understand correctly what you are asking for is a way to have your routes defined by your controller and action names instead of having to specify the route for each action, right?

Symfony2 doesn't provide that functionality out of the box, but it can be achieved with the third party bundle LswDefaultRoutingBundle.

After you installed and enabled the bundle you can setup your routing.yml like this:

_all:
    resource: "@SitesAllBundle"
    type:     default
    prefix:   /

Then if you debug your routes with

app/console router:debug

You should have something like:

[router] Current routes
Name                                                   Method Pattern
sites_allbundle.sites.index                            ANY    /sites/
sites_allbundle.sites.wow                              ANY    /sites/wow

Which matches /{prefix}/{controller}/{action}

Hope it helps!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.