symfony控制器必须返回响应(给定null)

时间:2019-09-11 05:47:01

标签: php symfony

有人可以帮助我理解错误之处吗, 以及我该如何解决?谢谢。

这是我的控制者:

namespace MegBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use MegBundle\Entity\message;
use Doctrine\ORM\Tools\Pagination\Paginator;
use Symfony\Component\HttpFoundation\Response;
class MegController extends Controller
    {
        /**
         * @Route("/Message/Board", name="MessageBoard")
         */

        public function postMeg()
        {
           $entityManager = $this->getDoctrine()->getEntityManager();
            $request = Request::createFromGlobals();
            if ($request->request->get("btnOK")) {
                $message = new message($request->request->get("txtName"));
                $message->setMes($request->request->get("txtMes"));
                $entityManager->persist($message);
                $entityManager->flush();
                return $this->redirect('/app_dev.php/Message/Board');
            }
        }
        public function deleteMeg()
        {
            $entityManager = $this->getDoctrine()->getEntityManager();
            $request = Request::createFromGlobals();
            if ($request->query->get("delete") != null) {
                $id = $request->query->get("delete");
                $message = $entityManager->find('MegBundle:message', $id);
                $entityManager->remove($message);
                $entityManager->flush();
                return $this->redirect('/app_dev.php/Message/Board');
            }
        }
        public function showMeg()
        {
            $entityManager = $this->getDoctrine()->getEntityManager();
            $request = Request::createFromGlobals();
            if ($request->query->get("DESC") != null) {
                $orderById = "DESC=ID";
                $orderby = 'DESC';
            }

            if ($request->query->get("DESC") == null) {
                $orderById = "ASC=ID";
                $orderby = 'ASC';
            }

            if ($request->query->get("page") == null) {
                $page = 1;
            }

            if ($request->query->get("page") != null) {
                $page = intval($request->query->get("page"));
            }

            $per = 5;
            $start = ($page-1)*$per;
            $query = $entityManager->createQueryBuilder()
                ->select("r")
                ->from("MegBundle:message", "r")
                ->orderBy('r.id', $orderby)
                ->setFirstResult($start)
                ->setMaxResults($per);
            $data = $query->getQuery()->getResult();
            $paginator = new Paginator($query, $fetchJoinCollection = false);
            $count = count($paginator);
            $pages = ceil($count/$per);
            return new Response(include '/usr/share/nginx/html/symfony/Symfony/src/MegBundle/Resources/views/MesView.php');  
        }  
}  

我要

控制器必须返回响应(给定为空)。您是否忘记在控制器中的某处添加return语句?

我不知道如何解决它。有人可以帮我吗?

添加所有使用语句和注释

1 个答案:

答案 0 :(得分:1)

注意:命名,重定向方法和许多事情都需要在代码中更正。 您的postMeg()和deletMeg()操作中只有一个return语句。 然后,如果此返回条件不完整,则返回值为null。这对于控制器来说是不期望的。如果条件不完整,可以通过设置返回值来避免这种情况。