Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my Symfony route is not working?

Tags:

php

symfony

I made a new Symfony2 bundle and deleted the Acme bundle.

Then I created a new Controller (MainController.php):

<?php
namespace My\BlogBundle\Controller;

class MainController extends Controller
{
    /**
     * @Route("/", name="index")
     * @Template()
     */
    public function indexAction()
    {

        return array();
    }

And a simple view: (Main/index.html.twig) which only contains a hello. My routing.yml is empty. When I run the whole project I get:

No route found for "GET /"
404 Not Found - NotFoundHttpException
1 linked Exception: ResourceNotFoundException »

What is wrong here and how to solve it?

Here is my routing debug:

\Symfony>php app/console router:debug
[router] Current routes
Name                     Method Pattern
_wdt                     ANY    /_wdt/{token}
_profiler_search         ANY    /_profiler/search
_profiler_purge          ANY    /_profiler/purge
_profiler_info           ANY    /_profiler/info/{about}
_profiler_import         ANY    /_profiler/import
_profiler_export         ANY    /_profiler/export/{token}.txt
_profiler_phpinfo        ANY    /_profiler/phpinfo
_profiler_search_results ANY    /_profiler/{token}/search/results
_profiler                ANY    /_profiler/{token}
_profiler_redirect       ANY    /_profiler/
_configurator_home       ANY    /_configurator/
_configurator_step       ANY    /_configurator/step/{index}
_configurator_final      ANY    /_configurator/final

I also cleared the cache with no success.

Here is the routes.yml:

my_blog:
    resource: "@MyBlogBundle/Resources/config/routing.yml"
    prefix:   /

and the routing.yml in MyBlogBundle/Resources/config/routing.yml is empty.

like image 909
gurehbgui Avatar asked Jan 20 '13 18:01

gurehbgui


People also ask

How does Symfony routing work?

Symfony provides a Routing component which allows us, for a HTTP request/URL, to execute a specific function (also known as "Controller"). Note: Controllers must be a callable, for example: an anonymous function: $controller = function (Request $request) { return new Response() }; .

What are annotations Symfony?

annotations are literally configuration inside PHP comments. If you don't like them, you can always use YAML or XML instead: Symfony is super flexible.


2 Answers

The way your routes.yml is setup, you are requesting the routing.yml file from your bundle.

If you want to use annotations to manage the routes in your bundle, you have to write the routes.yml the following way:

my_blog:
    resource: "@MyBlogBundle/Controller/MainController.php"
    prefix:   /
    type:     annotation

And your controller needs to include the Route class from the FrameworkExtraBundle:

<?php
namespace My\BlogBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class MainController extends Controller
{
    /**
     * @Route("/", name="index")
     * @Template()
     */
    public function indexAction()
    {
        return array();
    }
}

This assumes you have installed the SensioFrameworkExtraBundle (http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/index.html#installation).

More information on the route annotation: http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing.html

like image 193
cheesemacfly Avatar answered Oct 11 '22 19:10

cheesemacfly


You also could declare your routing for your annotation with:

blog_index:
    path: /
    defaults: {_controller:BlogBundle:index}

in your BlogBundle/config/routing.yml

and in root, set

blog:
    resource: "@BlogBundle/Resources/config/routing.yml"
    prefix:   /

then in your MainController, the annotation should work:

..use Symfony\Component\Routing\Annotation\Route;

       /**
         * @Route("/", name="blogindex")
         */
like image 37
aurny2420289 Avatar answered Oct 11 '22 19:10

aurny2420289