Example #1
0
 /**
  * Allow the user to add a article by passing the back office, the service create a Form using the
  * ArticleType and submit the Form, the Slug service is used to change the format of the article titre, if
  * everything is matched, the service persist the entity and save a flash message in the session.
  *
  * @param Request $request
  * @param $categorie
  *
  * @return mixed
  */
 public function addArticle(Request $request, $categorie)
 {
     $article = new Article();
     $article->setDatePublication(new \Datetime());
     $article->setCategorie($categorie);
     $user = $this->user->getToken()->getUser();
     $article->setAuteur($user);
     $article->setOnline(true);
     $form = $this->formbuilder->create(ArticleType::class, $article);
     $form->handleRequest($request);
     if ($form->isSubmitted() && $form->isValid()) {
         $slug = $this->slugify($article->getTitre());
         $article->setSlug($slug);
         $this->doctrine->persist($article);
         $this->doctrine->flush();
         $this->session->getFlashBag()->add('success', 'Article enregistré.');
     }
     return $form;
 }