public function __invoke($id = null) { $request = $this->getRequest(); if ($request->getMethod() == 'POST') { $posts = $request->request; $session = $this->getSession(); try { // 获取当前用户数据 if (!isset($session['auth'])) { throw new \Exception('请先登录再进行评论'); } $user_auth = $session['auth']; $content = $posts->get('content'); if (strlen(trim($content)) < 1) { throw new \Exception('评论内容不能为空'); } $comment_id = $posts->get('comment_id'); $comment_id = intval($comment_id); $comment = new CommentModel(); $comment->userId = $user_auth['id']; $comment->articleId = $id; $now = time(); $comment->createTimestamp = $now; $comment->updateTimestamp = $now; $comment->commentId = $comment_id; $comment->content = $content; // 保存 CommentModel::createComment($comment); } catch (\Exception $e) { $session->addFlash('error', $e->getMessage()); } return new RedirectResponse($this->generateUrl('www_article', array('id' => $id))); } return $this->render('comment/post.html.twig', array('id' => $id)); }
public function __invoke($id = null) { $comment_id = $id; $comment = CommentModel::getComment($comment_id); // 获取用户数据 $comment_user_id = $comment['user_id']; /** @var \Tachigo\User\Aware\Component\UserAwareDispatcher $dispatcher */ $dispatcher = $this->getContainer()->tag('tachigo_user_dispatcher'); /** @var \Tachigo\User\Aware\Hook\UserHook $hook */ $hook = $this->getContainer()->tag('tachigo_user_hook.user'); $hook->setUserId($comment_user_id); $dispatcher->fire($hook); $comment['user'] = $hook->getResults(); $request = $this->getRequest(); if ($request->getMethod() == 'POST') { $posts = $request->request; $session = $this->getSession(); try { // 获取当前用户数据 if (!isset($session['auth'])) { throw new \Exception('请先登录再进行评论'); } $user_auth = $session['auth']; $content = $posts->get('content'); if (strlen(trim($content)) < 1) { throw new \Exception('评论内容不能为空'); } $c = new CommentModel(); $c->userId = $user_auth['id']; $c->articleId = $comment['article_id']; $now = time(); $c->createTimestamp = $now; $c->updateTimestamp = $now; $c->commentId = $comment_id; $c->content = $content; // 保存 CommentModel::createComment($c); } catch (\Exception $e) { $session->addFlash('error', $e->getMessage()); } return new RedirectResponse($this->generateUrl('www_article', array('id' => $comment['article_id']))); } return $this->render('comment/reply.html.twig', array('comment' => $comment)); }