gurehbgui
gurehbgui

Reputation: 14674

why my symfony routing is not working?

hi i have a problem with my routing in symfony. my routing_dev looks like:

_test:
    resource: "@MyBundle/Controller/DefaultController.php"
    type:     annotation
    prefix:   /test

then i have a controller:

/**
 * @Route("/", name="_test")
 * @Template()
 */
public function indexAction($name)
{
    return array('name' => $name);
}

and when i try to run the /test i get the message:

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

i dont know where the error is. i need your help. how to find the problem?

EDIT: I'm working with the app_dev.php not app.php

Upvotes: 4

Views: 30599

Answers (5)

Krishna
Krishna

Reputation: 1137

If you are using Symfony 4 you just need to install doctrine/annotations package. To download it, in your terminal run:

composer require annotations

The command will download the package into your vendor folder and add new annotations.yaml file under config/routes folder. Make sure annotations.yaml has following lines:

controllers:
    resource: ../../src/Controller/
    type: annotation

You do not need any additional route settings inside config/routes.yaml file.

Upvotes: 1

Flask
Flask

Reputation: 4996

you explicit have to set the route for an action. not just for the whole controller.

your routing.yml should look like:

mybundle_controller:
    resource: "@MyBundle/Controller"
    type:     annotation
    prefix:   /test

your controller like:

/**
 * @Route("/") #Note you can omit this, since you set the prefix in the routing.yml
 * 
 */
public function indexAction($name)
{
    /**
     * @Route("/", name="_test"
     * @Template()
     */
    return array('name' => $name);
}

try to run php app/console router:debug

it should return you something like

_test                   ANY      /test/

Upvotes: 0

jeremieca
jeremieca

Reputation: 1188

First, you must be sure that routing.yml import the file routing_dev.yml.

Then, you must define the $name variable passed in argument in the route. If this argument is optional, you can give it a default value.

Moreover, note that you can put the prefix directly in the annotation.

/**
 * @Route("/blog")
 */
class TheClassController extends Controller
{
    /**
     * @Route("/{name}", name="_test", defaults={"name" = "Jean"})
     * @Template()
     */
    public function indexAction($name)
    {
         return array('name'=>$name);
    }
}

After, you can run run the command php app/console router:debug for test the route.

Upvotes: 3

Mark
Mark

Reputation: 1754

Are you actually typing into your url /test? Because your route is just "/", the name you have given is used to generate that route from a template. If you want the url to be /test you should change your route parameter to @Route("/test", name="test")

Upvotes: 0

Ziumin
Ziumin

Reputation: 4860

Maybe you have to use http://your.site/app_dev.php/test url. Or copy your routing information to routing.yml (not routing_dev.yml)

Upvotes: 0

Related Questions