Symfony defining routes in the controller
@Route ("/lucky/number") #必须用双引号
@Route ("/api/get-lucky-number/{number}") #{number} is a parameter
ROUTING.YML Configuring routing
API:
path:/API/{_locale}/{year}/{title}.{ _format}
defaults: {_controller: appbundle: Api:getjson, _format: html}
requirements:
_locale: en| Fr
_format: html| RSS
year: \d+
schemes: [http]
Show:
path:/show. JSON
defaults: {_controller: appbundle: api:showurl}
schemes: [http]
TPL:
path:/lucky/ number
defaults: {_controller:appbundle:Api: number}
schemes: [http]
- API: An alias that represents that reason, using generate && GenerateURL with the controller can be used to
- Path: Represents access to the URL, where parameters can be passed with {}, passed to the parameters can be used in the controller action parameters
- Defaults: Indicates the corresponding to the controller and method, Appbundle represents the controller directory, the API represents the controller, Getjson represents the request method
- Requirements: Represents a parameter to a validation method, where | represents ' or ', \d+ to match a number
- Schemes: Indicates the corresponding to request mode, HTTP | https
Below is a description of how to use this to router in the controller
# $year, $title is the {} to parameter, $_controller represents the controller, $_route corresponds to the alias Api,show, the TPL
Public function getjsonaction($title, $year, $_locale, $_format, $_ Controller, $_route)
{
s = [];
for ($i = 0; $i < ; $i+ +)
{
s[] = rand (0, +);
}
$other = [ ' title ' = $title ' year ' = $year ' _locale ' = $_locale ' _format ' = $_ Format ' _controller ' = $_controller = $_route
return New Jsonresponse ($other);
}
/**
#运行结果
{
Title: "API",
Year: "2014",
_locale: "en",
_format: "HTML",
_controller: "Appbundle\controller\apicontroller::getjsonaction",
_route: "API"
}
*/
Public function showurlaction()
{
//Get the corresponding URL
$params = $this->get ('router ')->match ('/lucky/number ');
//Generate URI
$uri = $this->get ('router ')->generate (' API '), [
' year ' = ' 2022 ',
' title ' = ' en ',
]);
//Generate a URL with a domain name
$url = $this->generateurl (' API ', [
' year ' = ' 2022 ',
' title ' = ' fr ',
' _format ' = ' html '
], Urlgeneratorinterface::absolute_url);
$uri = ";
return New Jsonresponse ([' params ' = = $params, ' uri ' = = $uri, ' url ' = = $url]);
}
/**
{
Params: {
_controller: "Appbundle\controller\apicontroller::numberaction",
_route: "App_api_number"
},
URI: "/api/en/2022/en",
URL: "Http://127.0.0.1:8000/api/en/2022/fr"
}
*/
- $this->get (' router ')->generate (' API ', ...) means getting a URI
- $this->generateurl (' API ',, Urlgeneratorinterface::absolute_url) is the generation of absolute to URL
Me && symfony3 (routing)