简体   繁体   中英

Symfony : why am I not allowed to combine “type” with “defaults” in routing.yml

Short version of the question : Symfony's error message explains that I cannot use the type key in conjunction with defaults , I can only use with resources (see details below). Why is it so ? How can I fix it ?

Detailed version :

Here is the exact sequence of what I did in Symfony :

composer create-project symfony/framework-standard-edition sym-book/ '~2.5'
php app/console generate:bundle --namespace=MiddleMan/BookBundle --format=yml

Edit the contents of src/MiddleMan/BookBundle/Resources/config/routing.yml to

middle_man_book_example:
    path:     /example
    defaults: { _controller: MiddleManBookBundle:Lucky:number }
    type: annotation

In directory src/MiddleMan/BookBundle/Controller , rename DefaultController.php to LuckyController.php , and edit its contents to

namespace MiddleMan\BookBundle\Controller;

class LuckyController
{
    /**
     * @Route("/lucky/number")
     */
    public function numberAction()
    {
        $number = rand(0, 100);

        return new Response(
            '<html><body>Lucky number: '.$number.'</body></html>'
        );
    }
}

Type localhost:8000/example/lucky/number in my browser.

The error message is as follows :

The "type" key for the route definition "middle_man_book_example" in 
"/Users/ewandelanoy/Documents/Math_Software/Symfony_stuff/sym-book/src/MiddleMan/BookBundle/Resources/config/routing.yml" 
is unsupported. It is only available for imports in combination with 
the "resource" key in 
/Users/ewandelanoy/Documents/Math_Software/Symfony_stuff/sym-book/src/MiddleMan/BookBundle/Resources/config/routing.yml (
which is being imported from "/Users/ewandelanoy/Documents/Math_Software/Symfony_stuff/sym-book/app/config/routing.yml")

If you are using type: annotation you don't need, and, actually, must not, to store defaults section in routing.yml. Defaults section would be configured for each route in its own annotation, route annotation would look like:

/**
 * Sample route
 * @Route("/{id}", name="sample-route-name", defaults={"id" = 1})
 * //defaults section is there to provide default values
 *
 * @Method({"GET", "POST"}) // not actually needed, default - GET
 */

And yml routing configuration would look like:

sample_bundle_routing:
    resource: "@YourBundle/Controller/"
    type:     annotation
    prefix:   /some_prefix/

UPD This yml config should be put in main routing.yml . But you could use another variant. In main routing.yml put this:

sample_bundle_routing:
    resource: "@YourBundle/Resources/config/routing.yml"

Then in the routing.yml that you put in main routing.yml as a resource you can write:

sample_bundle_routing:
    resource: "@YourBundle/Controller/"
    type:     annotation
    prefix:   /some_prefix/

I have a question: why do you need to use both yml and annotation routing config? I mean that you write in your comment below: "But I need to edit the routing.yml file in my directory also, to give the controller name and action method name.". If you use annotation routing type - you could do it there no need to do configuration for annotation routing in yml. I mean: you do routing either as annotations for each action or as strings in bundle/main routing.yml , not both at the same time! If you use annotation routing, the only thing you need to put in yml - is where to look for routing config (in my example it was "@YourBundle/Controller/" path).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM