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

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

PHP中文网
PHP中文网原创
2016-05-25 17:12:101219浏览

           

<?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