Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Creating random articles
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use App\Entity\Article;
class ArticleController extends Controller
{
/**
* @Route("/random-article", name="create_random_article")
*/
public function createRandomArticle()
{
$article = new Article();
$title = bin2hex(openssl_random_pseudo_bytes(3)); // Titolo random
$article->setTitle($title);
$article->setSlug($title);
$article->setContent("Un altro articolo inutile");
$now = new \DateTime();
$article->setCreationDateTime($now);
$article->setLastModificationDateTime(null);
$em = $this->getDoctrine()->getManager();
$em->persist($article);
$em->flush();
return $this->render('article.html.twig', [
'$article' => $article,
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.