use Symfony\Component\HttpFoundation\Response;
public function helloAction()
{
return new Response('Hello world!');
}
// src/Acme/HelloBundle/Controller/HelloController.php
namespace Acme\HelloBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
class HelloController
{
public function indexAction($name)
{
return new Response('<html><body>Hello '.$name.'!</body></html>');
}
}
# app/config/routing.yml
hello:
pattern: /hello/{name}
defaults: { _controller: AcmeHelloBundle:Hello:index }
<!-- app/config/routing.xml -->
<route id="hello" pattern="/hello/{name}">
<default key="_controller">AcmeHelloBundle:Hello:index</default>
</route>
// app/config/routing.php
$collection->add('hello', new Route('/hello/{name}', array(
'_controller' => 'AcmeHelloBundle:Hello:index',
)));
<?php
// src/Acme/HelloBundle/Controller/HelloController.php
namespace Acme\HelloBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class HelloController extends Controller
{
public function indexAction($name)
{
// ...
}
}
# app/config/routing.yml
hello:
pattern: /hello/{first_name}/{last_name}
defaults: { _controller: AcmeHelloBundle:Hello:index, color: green }
<!-- app/config/routing.xml -->
<route id="hello" pattern="/hello/{first_name}/{last_name}">
<default key="_controller">AcmeHelloBundle:Hello:index</default>
<default key="color">green</default>
</route>
// app/config/routing.php
$collection->add('hello', new Route('/hello/{first_name}/{last_name}', array(
'_controller' => 'AcmeHelloBundle:Hello:index',
'color' => 'green',
)));
public function indexAction($first_name, $last_name, $color)
{
// ...
}
public function indexAction($last_name, $color, $first_name)
{
// ..
}
public function indexAction($first_name, $last_name, $color, $foo)
{
// ..
}
public function indexAction($first_name, $color)
{
// ..
}
use Symfony\Component\HttpFoundation\Request;
public function updateAction(Request $request)
{
$form = $this->createForm(...);
$form->bindRequest($request);
// ...
}
// src/Acme/HelloBundle/Controller/HelloController.php
namespace Acme\HelloBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
class HelloController extends Controller
{
public function indexAction($name)
{
return new Response('<html><body>Hello '.$name.'!</body></html>');
}
}
public function indexAction()
{
return $this->redirect($this->generateUrl('homepage'));
}
public function indexAction()
{
return $this->redirect($this->generateUrl('homepage'), 301);
}
use Symfony\Component\HttpFoundation\RedirectResponse;
return new RedirectResponse($this->generateUrl('homepage'));
public function indexAction($name)
{
$response = $this->forward('AcmeHelloBundle:Hello:fancy', array(
'name' => $name,
'color' => 'green'
));
// further modify the response or return it directly
return $response;
}
public function fancyAction($name, $color)
{
// ... create and return a Response object
}
$httpKernel = $this->container->get('http_kernel');
$response = $httpKernel->forward('AcmeHelloBundle:Hello:fancy', array(
'name' => $name,
'color' => 'green',
));
$content = $this->renderView('AcmeHelloBundle:Hello:index.html.twig', array('name' => $name));
return new Response($content);
$templating = $this->get('templating');
$content = $templating->render('AcmeHelloBundle:Hello:index.html.twig', array('name' => $name));
$templating->render('AcmeHelloBundle:Hello/Greetings:index.html.twig', array('name' => $name));
// index.html.twig 存放于 Resources/views/Hello/Greetings 目录.
$request = $this->getRequest();
$templating = $this->get('templating');
$router = $this->get('router');
$mailer = $this->get('mailer');
$ php app/console container:debug
public function indexAction()
{
$product = // retrieve the object from database
if (!$product) {
throw $this->createNotFoundException('The product does not exist');
}
return $this->render(...);
}
throw new \Exception('Something went wrong!');
$session = $this->getRequest()->getSession();
// 为用户的后一个请求使用存储一个属性
$session->set('foo', 'bar');
// 在另一个controller中为另一个请求获取该属性
$foo = $session->get('foo');
// 设置用户的本地化语言
$session->setLocale('fr');
public function updateAction()
{
$form = $this->createForm(...);
$form->bindRequest($this->getRequest());
if ($form->isValid()) {
// 做些排序处理
$this->get('session')->setFlash('notice', 'Your changes were saved!');
return $this->redirect($this->generateUrl(...));
}
return $this->render(...);
}
{% if app.session.hasFlash('notice') %}
<div class="flash-notice">
{{ app.session.flash('notice') }}
</div>
{% endif %}
<?php if ($view['session']->hasFlash('notice')): ?>
<div class="flash-notice">
<?php echo $view['session']->getFlash('notice') ?>
</div>
<?php endif; ?>
//创建一个简单的Response对象,默认状态码为200
$response = new Response('Hello ' .$name, 200);
//创建一个基于JSON的Response对象,状态码也为200
$response = new Response(json_encode(array('name'=>$name)));
$response->headers->set('content-type','application/json');
$request = $this->getRequest();
$request->isXmlHttpRequest(); // 判断是不是Ajax请求
$request->getPreferredLanguage(array('en','fr'));
$request->query->get('page'); // 获取$_GET 参数
$request->request->get('page'); //获取$_POST参数
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有