标签:form 如何使用 cal back require 配置 tle for round
symfony 在控制器定义路由
@Route("/lucky/number") #必须用双引号
@Route("/api/get-lucky-number/{number}") #{number} 为传参
routing.yml 配置路由
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]
下面介绍下在控制器中,如何使用这样到router
# $year,$title 就是{}中到参数, $_controller 表示控制器, $_route 对应别名 api,show, tpl
public function getJsonAction($title, $year, $_locale, $_format, $_controller, $_route)
{
$numbers = [];
for ($i = 0; $i < 10; $i++)
{
$numbers[] = rand(0, 100);
}
$other = [‘title‘ => $title, ‘year‘ => $year, ‘_locale‘ => $_locale, ‘_format‘ => $_format, ‘_controller‘ => $_controller, ‘_route‘ => $_route];
return new JsonResponse($other);
}
/**
#运行结果
{
title: "api",
year: "2014",
_locale: "en",
_format: "html",
_controller: "AppBundle\Controller\ApiController::getJsonAction",
_route: "api"
}
*/
public function showUrlAction()
{
//获取对应的url
$params = $this->get(‘router‘)->match(‘/lucky/number‘);
//生成uri
$uri = $this->get(‘router‘)->generate(‘api‘, [
‘year‘ => ‘2022‘,
‘title‘ => ‘en‘,
]);
//生成带域名的url
$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"
}
*/
标签:form 如何使用 cal back require 配置 tle for round
原文地址:http://www.cnblogs.com/zeopean/p/symfony3.html