symfony框架学习

2016-10-26 11:35:46来源:cnblogs.com作者:spectrelb人点击

http://mozilla.com.cn/thread-52722-1-1.html安装网页翻译插件


symfony2是基于php的web框架

http://www.chrisyue.com/symfony2-in-action-day-1.html

Symfony2框架实战教程

 window下安装在环境变量里面配置php,然后用php在cmd里面执行php -r "file_put_contents('symfony', file_get_contents('https://symfony.com/installer'));"
上句执行好了,在执行这句,是创建一个新的symfony工程:php symfony new my_project_name
 form表单的写法2点级版本的写法 $form=$this->createFormBuilder($task)            ->add('task','text')            ->add('dueDate','date')            ->add('save','submit',array('label'=>'Create Task'))            ->getForm();3.0以上的写法
$form = $this->createFormBuilder($task)
->add('task', TextType::class)
->add('dueDate', DateType::class)
->add('save', SubmitType::class, array('label' => 'Create Task'))
->getForm();
需要引入
use Symfony/Component/Form/Extension/Core/Type/TextType;
use Symfony/Component/Form/Extension/Core/Type/DateType;
use Symfony/Component/Form/Extension/Core/Type/SubmitType;
return $this->redirectToRoute('login_index');重定向
注册用的是orm映射
//这个方法是创建出一个注册的form表单
public function indexAction(Request $request)
{
$user = new User();

$form = $this->createFormBuilder($user)
->setAction($this->generateUrl('register_register'))
->setMethod('post')
->add('username', TextType::class)
->add('password', PasswordType::class)
->add('email', TextType::class)
->add('phone', TextType::class)
->add('gender', TextType::class)
->add('save', SubmitType::class, array('label' => 'Register'))
->getForm();

return $this->render('register/index.html.twig', array(
'form' => $form->createView()
));
}
//V层twig展现出来
{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}
//这个方法是得到form表单是数据,得到的form是一个对象,要转化成数组,然后插入到数据库
public function registerAction(Request $request)
{

$user = new User();

$user->setLoginIp($request->getClientIP());//得到本机的IP地址
$user->setLoginTimes(new /DateTime('now'));//得到当前时间
$form = $this->createFormBuilder($user)
->add('username', TextType::class)
->add('password', PasswordType::class)
->add('email', TextType::class)
->add('phone', TextType::class)
->add('gender', TextType::class)
->getForm();

$form->handleRequest($request);

$data = $form->getData();
json_encode($data);

$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();

return $this->redirectToRoute('login_index');
}
 在控制器里跳同控制器的方法 还带传参
 
 

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台