Skip to content

Instantly share code, notes, and snippets.

@tatma
Last active December 8, 2018 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tatma/d5dec787c7da15b433d58591a2272f20 to your computer and use it in GitHub Desktop.
Save tatma/d5dec787c7da15b433d58591a2272f20 to your computer and use it in GitHub Desktop.
Gestione del submit
<?php
namespace App\Controller;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use App\Form\ArticleType;
use App\Entity\Article;
class ArticleController extends Controller
{
/**
* @Route("/create-new", name="article_new")
*/
public function newArticle(Request $request)
{
$article = new Article();
$form = $this->createForm(ArticleType::class, $article);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// Il form è stato sottomesso
$now = new \DateTime();
$article->setCreationDateTime($now);
$article->setLastModificationDateTime(null);
$slug = $this->slugify($article->getTitle());
$article->setSlug($slug);
$em = $this->getDoctrine()->getManager();
$em->persist($article);
$em->flush();
return $this->redirectToRoute('article_show', [
'slug' => $article->getSlug()
]);
}
return $this->render('Backend/new.html.twig', [
'form' => $form->createView()
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment