/** * @param $id * @param $action * @param Request $request * @Route("/article/{action}/{id}", name="articleEdit", * defaults={"id": 0}, * requirements={ * "action": "new|edit", * "id": "\d+" * }) * @Method({"GET", "POST"}) * @Template("AppBundle:admin/form:article.html.twig") * * @return Response */ public function editArticleAction($id, $action, Request $request) { $em = $this->getDoctrine()->getManager(); if ($action == "edit") { $article = $em->getRepository('AppBundle:Article')->find($id); $title = 'Edit article id: ' . $id; $this->denyAccessUnlessGranted('edit', $article); } else { $article = new Article(); $user = $this->get('security.token_storage')->getToken()->getUser(); $article->setUser($user); $title = 'Create new article'; $this->denyAccessUnlessGranted('create', $article); } $form = $this->createForm(ArticleType::class, $article, ['em' => $em, 'action' => $this->generateUrl('articleEdit', ['action' => $action, 'id' => $id]), 'method' => Request::METHOD_POST])->add('save', SubmitType::class, array('label' => 'Save')); if ($request->getMethod() == 'POST') { $form->handleRequest($request); if ($form->isValid()) { $em->persist($article); $em->flush(); return $this->redirectToRoute('articlesAdmin'); } } return ['title' => $title, 'form' => $form->createView()]; }
/** * Creates a new Article entity. * * @Route("/", name="articles_create") * @Method("POST") * @Template("Article/new.html.twig") * * @param Request $request * * @return array|\Symfony\Component\HttpFoundation\RedirectResponse */ public function createAction(Request $request) { $article = new Article(); $article->setUser($this->getUser()); $form = $this->createCreateForm($article); $form->handleRequest($request); if ($form->isValid()) { $em = $this->getDoctrine()->getManager(); $em->persist($article); $em->flush(); return $this->redirect($this->generateUrl('homepage')); } return ['article' => $article, 'new_form' => $form->createView()]; }
/** * Chức năng: Tạo ra một bài viết mới * @Route("/user/new", name = "new_article") */ public function newAction(Request $request) { $user = new User(); $user = $this->getDoctrine()->getManager()->getRepository('AppBundle:User')->findOneById(1); $article = new Article(); $article->setPublishedAt(new \DateTime('today')); $article->setNumView(0); $article->setNumLike(0); $article->setNumUnlike(0); $article->setNumShare(0); $article->setUser($user); $form = $this->createFormBuilder($article)->add('title', 'text')->add('summary', 'textarea')->add('content', 'textarea')->add('publishedAt', 'date')->add('tag', 'text')->add('specialTag', 'text')->add('create', 'submit', array('label' => 'Create News'))->getForm(); $form->handleRequest($request); if ($form->isValid()) { $entityManager = $this->getDoctrine()->getManager(); $entityManager->persist($article); $entityManager->flush(); return $this->redirectToRoute('show_article'); } return $this->render('default/new.html.twig', array('form' => $form->createView())); }
/** * @param Request $request * @return \Symfony\Component\HttpFoundation\RedirectResponse * @Route("/", name="create_article") * @Method("POST") * @Template("AppBundle:Article:form.html.twig") */ public function createAction(Request $request) { if (!$this->get('security.authorization_checker')->isGranted('ROLE_MODERATOR')) { throw $this->createAccessDeniedException(); } $user = $this->getUser(); $article = new Article(); $article->setUser($user); $form = $this->createForm(ArticleType::class, $article, ['action' => $this->generateUrl('create_article'), 'method' => 'POST'])->add('save', SubmitType::class, ['label' => 'Create']); $form->handleRequest($request); if ($form->isValid()) { $em = $this->getDoctrine()->getManager(); $em->persist($article); $em->flush(); return $this->redirect($this->generateUrl('show_article', ['slug' => $article->getSlug()])); } return ['form' => $form->createView()]; }
/** * Post a new article. * * { "article": { "title": "Lorem", "body": "ipsum" } } * * @param Request $request * @param $user_id * * @return View|Response * * @FOSRest\View() * @FOSRest\Post( * "/users/{user_id}/articles/", * requirements = { * "user_id": "\d+" * } * ) * @Nelmio\ApiDoc( * input = ArticleType::class, * statusCodes = { * Response::HTTP_CREATED : "Created" * } * ) */ public function postArticleAction(Request $request, $user_id) { $em = $this->getDoctrine()->getManager(); $user = $em->getRepository('AppBundle:User')->find($user_id); if (!$user instanceof User) { throw new NotFoundHttpException(); } $article = new Article(); $article->setUser($user); $logger = $this->get('logger'); $logger->info($request); return $this->processArticleForm($request, $article); }
/** * Post a new article. * * { "article": { "title": "Lorem", "body": "ipsum" } } * * @param Request $request * @param $user_id * * @return View|Response * * @FOSRest\View() * @FOSRest\Post("/users/{user_id}/articles/", requirements = {"user_id": "\d+"}) * @Nelmio\ApiDoc( * input = "Artevelde\ApiBundle\Form\ArticleType", * statusCodes = { * Response::HTTP_CREATED: "Created" * } * ) */ public function postAction(Request $request, $user_id) { # HTTP method: POST # Host/port : http://www.nmdad3.arteveldehogeschool.local # # Path : /app_dev.php/api/v1/users/1/articles/ $em = $this->getDoctrine()->getManager(); $user = $em->getRepository('AppBundle:User')->find($user_id); if (!$user instanceof User) { throw new NotFoundHttpException(); } $article = new Article(); $article->setUser($user); return $this->processArticleForm($request, $article); }