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

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

PHP中文网
PHP中文网原創
2016-05-25 17:12:101213瀏覽

           

<?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);
        }
    }
}

                   

                   

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn