RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 810975
Accepted
Aleksey exec
Aleksey exec
Asked:2020-04-08 00:18:36 +0000 UTC2020-04-08 00:18:36 +0000 UTC 2020-04-08 00:18:36 +0000 UTC

如何在 symfony 中实现反馈表

  • 772

如何在symfony中实现一个反馈表,将用户的电话号码发送到邮件中,是否可以通过控制台做到这一点

php app/console

我想要更多的例子细节,因为我在symfony中完全是零 ,我不知道那里的一切是如何工作的——除了树枝模板

php
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    Mike Foxtech
    2020-04-08T05:13:34Z2020-04-08T05:13:34Z

    在 symfony3 上

    为联系表单创建 FormType

    以下类包含稍后将用于在控制器中创建表单的 ContactType。

    <?php
    // your-path-to-types/ContactType.php
    
    namespace myapplication\myBundle\Form;
    
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\OptionsResolver\OptionsResolver;
    use Symfony\Component\Form\Extension\Core\Type\TextType;
    use Symfony\Component\Form\Extension\Core\Type\TextareaType;
    use Symfony\Component\Form\Extension\Core\Type\EmailType;
    use Symfony\Component\Validator\Constraints\Email;
    use Symfony\Component\Validator\Constraints\NotBlank;
    
    class ContactType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
                ->add('name', TextType::class, array('attr' => array('placeholder' => 'Your name'),
                    'constraints' => array(
                        new NotBlank(array("message" => "Please provide your name")),
                    )
                ))
                ->add('subject', TextType::class, array('attr' => array('placeholder' => 'Subject'),
                    'constraints' => array(
                        new NotBlank(array("message" => "Please give a Subject")),
                    )
                ))
                ->add('email', EmailType::class, array('attr' => array('placeholder' => 'Your email address'),
                    'constraints' => array(
                        new NotBlank(array("message" => "Please provide a valid email")),
                        new Email(array("message" => "Your email doesn't seems to be valid")),
                    )
                ))
                ->add('message', TextareaType::class, array('attr' => array('placeholder' => 'Your message here'),
                    'constraints' => array(
                        new NotBlank(array("message" => "Please provide a message here")),
                    )
                ))
            ;
        }
    
        public function setDefaultOptions(OptionsResolver $resolver)
        {
            $resolver->setDefaults(array(
                'error_bubbling' => true
            ));
        }
    
        public function getName()
        {
            return 'contact_form';
        }
    }
    

    在 Twig 中创建视图

    现在视图(在这种情况下,将通过 twig 呈现的视图)需要作为验证的基础:

    {# contact.html.twig #}
    
    {{ form_start(form) }}
    
        <div>
            {{ form_widget(form.subject) }}
            {{ form_errors(form.subject) }}
        </div>
        <div>
            {{ form_widget(form.name) }}
            {{ form_errors(form.name) }}
        </div>
        <div>
            {{ form_widget(form.email) }}
            {{ form_errors(form.email) }}
        </div>
        <div>
            {{ form_widget(form.message) }}
            {{ form_errors(form.message) }}
        </div>
    
        {# Render CSRF token etc .#}
        <div style="display:none">
            {{ form_rest(form) }}
        </div>
    
        <input type="submit" value="Submit">
    
    {{ form_end(form) }}
    

    创建控制器

    现在,最重要的一点,将处理我们的表单的控制器。

    像往常一样,您的控制器操作应该已经在 routing.yml 文件中有一个路径,并且它以它为目标:

    myapplication_contact:
        path:     /contact
        defaults: { _controller: myBundle:Default:contact }
    

    最后,我们的控制器(带有联系动作)应该是这样的:

    <?php
    
    namespace myapplication\myBundle\Controller;
    
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Symfony\Component\HttpFoundation\Request;
    
    class DefaultController extends Controller
    {
        public function contactAction(Request $request)
        {
            // Create the form according to the FormType created previously.
            // And give the proper parameters
            $form = $this->createForm('myapplication\myBundle\Form\ContactType',null,array(
                // To set the action use $this->generateUrl('route_identifier')
                'action' => $this->generateUrl('myapplication_contact'),
                'method' => 'POST'
            ));
    
            if ($request->isMethod('POST')) {
                // Refill the fields in case the form is not valid.
                $form->handleRequest($request);
    
                if($form->isValid()){
                    // Send mail
                    if($this->sendEmail($form->getData())){
    
                        // Everything OK, redirect to wherever you want ! :
    
                        return $this->redirectToRoute('redirect_to_somewhere_now');
                    }else{
                        // An error ocurred, handle
                        var_dump("Errooooor :(");
                    }
                }
            }
    
            return $this->render('myBundle:Default:contact.html.twig', array(
                'form' => $form->createView()
            ));
        }
    
        private function sendEmail($data){
            $myappContactMail = 'mycontactmail@mymail.com';
            $myappContactPassword = 'yourmailpassword';
    
            // In this case we'll use the ZOHO mail services.
            // If your service is another, then read the following article to know which smpt code to use and which port
            // http://ourcodeworld.com/articles/read/14/swiftmailer-send-mails-from-php-easily-and-effortlessly
            $transport = \Swift_SmtpTransport::newInstance('smtp.zoho.com', 465,'ssl')
                ->setUsername($myappContactMail)
                ->setPassword($myappContactPassword);
    
            $mailer = \Swift_Mailer::newInstance($transport);
    
            $message = \Swift_Message::newInstance("Our Code World Contact Form ". $data["subject"])
            ->setFrom(array($myappContactMail => "Message by ".$data["name"]))
            ->setTo(array(
                $myappContactMail => $myappContactMail
            ))
            ->setBody($data["message"]."<br>ContactMail :".$data["email"]);
    
            return $mailer->send($message);
        }
    }
    
    • 2

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    是否可以在 C++ 中继承类 <---> 结构?

    • 2 个回答
  • Marko Smith

    这种神经网络架构适合文本分类吗?

    • 1 个回答
  • Marko Smith

    为什么分配的工作方式不同?

    • 3 个回答
  • Marko Smith

    控制台中的光标坐标

    • 1 个回答
  • Marko Smith

    如何在 C++ 中删除类的实例?

    • 4 个回答
  • Marko Smith

    点是否属于线段的问题

    • 2 个回答
  • Marko Smith

    json结构错误

    • 1 个回答
  • Marko Smith

    ServiceWorker 中的“获取”事件

    • 1 个回答
  • Marko Smith

    c ++控制台应用程序exe文件[重复]

    • 1 个回答
  • Marko Smith

    按多列从sql表中选择

    • 1 个回答
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Suvitruf - Andrei Apanasik 什么是空? 2020-08-21 01:48:09 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5