Home >php教程 >PHP源码 >Symfony2框架下一个简单Ajax留言板

Symfony2框架下一个简单Ajax留言板

PHP中文网
PHP中文网Original
2016-05-25 17:12:101213browse

           

<?php

namespace Dp\DriveplusBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Dp\DriveplusBundle\Entity\Messageboard;

class MessageController extends Controller
{    
    public function indexAction()
    {
        $em = $this->getDoctrine()->getEntityManager();
        $query = $em->createQuery(
             &#39;SELECT m FROM DpDriveplusBundle:Messageboard m ORDER BY m.id DESC&#39;
         );
        $comments = $query->getResult();
        $count = count($comments);
        return $this->render(&#39;DpDriveplusBundle:Message:index.html.twig&#39;, array(&#39;comments&#39;=>$comments, &#39;count&#39;=>$count));
    }
    
    public function ajaxAction()
    {      
        $request = $this->getRequest();
        $ret = &#39;&#39;;
             
        if (&#39;POST&#39; === $request->getMethod()) {       
            $name = $request->get(&#39;name&#39;);
            $message = $request->get(&#39;message&#39;);          
            $ret = &#39;姓名:&#39;.$name.&#39;<br/> 留言内容:&#39;.$message.&#39;<hr/>&#39;;
            
            $comment = new Messageboard();
            $comment->setName($name);
            $comment->setMessage($message);
            $em = $this->getDoctrine()->getEntityManager();
            $em->persist($comment);       
            $em->flush();
            
            return new Response($ret);
        }
    }
}

                   

                   

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:阴历-农历-转换类Next article:PHP句法规则详解