framework-web:symfony:routing

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentes Révision précédente
Prochaine révision
Révision précédente
framework-web:symfony:routing [2018/01/31 01:45] – [Paramètres de routage spéciaux] jcheronframework-web:symfony:routing [2019/08/31 14:21] (Version actuelle) – modification externe 127.0.0.1
Ligne 116: Ligne 116:
 } }
 </sxh> </sxh>
- 
  
 ===== Valeur par défaut ===== ===== Valeur par défaut =====
Ligne 134: Ligne 133:
     {     {
         // ...         // ...
 +    }
 +}
 +</sxh>
 +
 +===== Méthode HTTP =====
 +L'ajout de l'attribut **methods** à la route permet de spécifier la/les méthode(s) utilisables : 
 +
 +<sxh php;title:src/Controller/BlogApiController.php>
 +namespace App\Controller;
 +
 +// ...
 +
 +class BlogApiController extends Controller
 +{
 +    /**
 +     * @Route("/api/posts/{id}", methods={"GET","HEAD"})
 +     */
 +    public function show($id)
 +    {
 +        // ... return a JSON response with the post
 +    }
 +
 +    /**
 +     * @Route("/api/posts/{id}", methods="PUT")
 +     */
 +    public function edit($id)
 +    {
 +        // ... edit a post
     }     }
 } }
Ligne 192: Ligne 219:
  
 ==== URL avec paramètres ==== ==== URL avec paramètres ====
 +Les variables supplémentaires sont ajoutées en tant que query string :
 <sxh php> <sxh php>
-$this->router->generate('blog', array(+$this->generateUrl('blog_list', array(
     'page' => 2,     'page' => 2,
     'category' => 'Symfony',     'category' => 'Symfony',
Ligne 201: Ligne 228:
 </sxh> </sxh>
  
 +==== Génération depuis un service ====
  
 +Il est nécessaire d'injecter une instance de **UrlGeneratorInterface**
  
 +<sxh php;title:src/Service/SomeService.php>
 +use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
 +
 +class SomeService
 +{
 +    private $router;
 +
 +    public function __construct(UrlGeneratorInterface $router)
 +    {
 +        $this->router = $router;
 +    }
 +
 +    public function someMethod()
 +    {
 +        $url = $this->router->generate(
 +            'blog_show',
 +            array('slug' => 'my-blog-post')
 +        );
 +        // ...
 +    }
 +}
 +</sxh>
  • framework-web/symfony/routing.1517359531.txt.gz
  • Dernière modification : il y a 6 ans
  • (modification externe)