symfony2学习笔记(8)——控制器(Controller)

Request

$request = $this->getRequest();
$request->isXmlHttpRequest(); //是否是ajax请求
$request->getClientIp();
$request->get('a');//如果get&&post都有a变量,优先get请求
$params = $request->request->all(); //post参数
$params['value1'];
$params['value2'];
$params = $request->query->all(); //get参数
$params['value1'];
$params['value2']

Response

use Symfony\Componen\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
          
return new JsonResponse(array('a')); //继承自response
return new Response("13323");
return new RedictResponse("http://www.xuejiehome.com"); //继承自response


Session

$this->getRequest()->getSession()->set("c",1000);
$this->getRequest()->getSession()->get("c");
$this->getRequest()->getSession()->getFlashBag()->add('notice','you have something wrong); //只会显示一次,马上销毁
$this->get('session')->setFlash('notice', 'Your changes were saved!');//这在处理一个表单时非常有用,你想重定向和一个特定的信息显示在下一个请求中。这种类型的消息被称为Flash消息。

如果get到的session为空,可能app/cache/dev权限问题没有写进去,但不会报错


Form

(1)创建表单


// create a task and give it some dummy data for this example
$task = new Task();
$task->setTask('Write a blog post');
$task->setDueDate(new DateTime('tomorrow'));
          
$form = $this->createFormBuilder($task)
    ->setAction($this->generateUrl('target_route'))
    ->setMethod('GET')
    ->add('task', 'text')
    ->add('dueDate', 'date')
    ->add('checkbox', 'checkbox', array(
            'label' => '单选框:',
            'attr' => array(
                'alt' => '单选框',
                'style' => 'color:red'
            ),
            'empty_data'=>1,
            'data' => true //默认选中
        )
    )
    ->add('drop_down', 'choice', array(
            'label' => '下拉列表:',
            'attr' => array(
                'alt' => '下拉列表',
                'style' => 'color:red'
            ),
            'multiple' => false,
            'expanded' => false,
            'data' => 'b', //默认选中
            'choices' => array(
                'a' => 'a',
                'b' => 'b',
                2 => 2
            )
        )
    )
    ->add('multi_checkbox', 'choice', array(
            'label' => '多选复选框:',
            'attr' => array(
                'alt' => '多选复选框',
                'style' => 'color:red'
            ),
            'multiple' => true,
            'expanded' => true,
            'data' => array(
                'a', 2
            ), //默认选中
            'choices' => array(
                'a' => 'a',
                'b' => 'b',
                2 => 2
            )
        )
    )
    ->add('input', null, array(
            'label' => '文本框:',
            'data' => '默认值'
        )
    )
    ->add('save', 'submit', array('label' => 'Create Task'))
    ->getForm();

(2)创建嵌套的表单:

$em = $this->getDoctrine()->getEntityManager();
      
//$article = new Article();
//$user = new User();
$user = $em->getRepository('PUserBundle:User')->find(1);
$article = $em->getRepository('TestBundle:Article')->findOneBy(array('id'=>1));
      
$builder = $this->createFormBuilder();
$form = $builder
    ->add(
        $builder->create('article','form',array('label'=>'文章信息','data'=>$article))
            ->add('title',null,array('label'=>'title'))
            ->add('content',null,array('label'=>'content'))
    )
    ->add(
        $builder->create('user','form',array('label'=>'用户信息','data'=>$user))
            ->add('username',null,array('label'=>'user'))
            ->add('password',null,array('label'=>'pass'))
    )
    ->add('submit','submit',array('label'=>'提交'))
    ->getForm();

效果:


(3)直接定位错误信息到某个字段

// 将错误信息定位在第(2)中用户信息——>username 和 password
$form->get('user')->get('username')->addError(new FormError('用户名已存在'));
$form->get('user')->get('password')->addError(new FormError('密码错误'));
    
// 将错误信息定位在第(1)中——>task
$form->get('task')->addError(new FormError('任务名称已存在'));

效果(具体样式不同框架自行调整):

表单其他字段类型讲解:http://www.xuejiehome.com/blread-1885.html


Service

service oriented architecture SOA

面向服务架构

Request也是一个服务

$this->container->get('request');

服务的详细讲解:http://www.xuejiehome.com/blread-1921.html


by 雪洁 2015-10-20 07:42:22 2403 views
我来说几句

相关文章