Shortcuts methods in Controller
1. 獲取服務
$this->get($serviceId);
2. 重定向
$this->redirectToRoute($routeName, $parameters, $status = 302);
3. 生成路由的url
$this->generateUrl($routeName, $parameters, $referenceType);
4. 返回一個json response
$this->json($data, $status = 200, $headers = array(), $context = array());
5. 添加一個flash message
$this->addFlash($type, $message);
6. 判斷是否受權進入某個方法
$this->isGranted('ROLE_ADMIN');
7. 判斷用戶是否受權,否,拋出異常
$this->denyAccessUnlessGranted('ROLE_EDIT', $item, 'You cannot edit this item.');
8. 手動判斷 CSRF token是否合法
$this->isCsrfTokenValid('token_id', $token);
9. 把請求轉發到其餘控制器和方法
$this->forward('GregwarCaptchaBundle:Captcha:generateCaptcha', ['key' => $key]);
//forward($controller, array $path = array(), array $query = array())
PS: 第二個參數爲route上佔位符參數,第三個爲其餘額外的參數
10. 文件下載(symfony >= 3.2)
$this->file();
//簡單用法示例
return $this->file($docPath, $saveName);
// 若是是pdf之類,直接顯示而不是下載,須要設置第三個參數ResponseHeaderBag::DISPOSITION_INLINE
// 還能夠直接接受一個File或者UploadedFile實例
//$samplePdf = new File('/sample.pdf');
//return $this->file($samplePdf);
11. 讀取配置參數
$this->getParameter('kernel.root_dir');