wiki/content/20201112121416-always_include_route_default_values.md

39 lines
724 B
Markdown
Raw Normal View History

2024-05-06 20:40:05 +00:00
---
2024-10-29 18:27:12 +00:00
date: 2020-11-12
2024-05-06 20:40:05 +00:00
id: 63017411-62dd-4586-86c5-4b0edc70114d
title: Always Include Route Default Values
---
# Description
Sometimes when generating URLs you have situations where you don't want
the default value in the URL.
# Syntax
``` php
/**
* @Route("/blog/{!page}", name="blog_list")
*/
public function list($page = 1)
{
// ...
}
```
``` php
$url = $router->generate('blog_list'); // $url = '/blog/1'
$url = $router->generate('blog_list', ['page' => 1]); // $url = '/blog/1'
$url = $router->generate('blog_list', ['page' => 7]); // $url = '/blog/7'
```
``` php
/**
* @Route("/blog/{page}.{!_format<html|json>?html}", name="blog_list")
*/
public function list($page = 1)
{
// ...
}
```