symfony框架學習,symfony框架
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');
}
在控制器裡跳同控制器的方法 還帶傳參