forked from Ippey/HowDoYouCodeToWorkWithDb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArticlesController.php
More file actions
72 lines (58 loc) · 1.68 KB
/
Copy pathArticlesController.php
File metadata and controls
72 lines (58 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
namespace App\Controller;
use App\Entity\Article;
use App\Form\ArticleType;
use App\Repository\ArticleRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/articles", name="articles_")
*/
class ArticlesController extends AbstractController
{
/**
* @var ArticleRepository
*/
private $repository;
/**
* @var EntityManagerInterface
*/
private $em;
public function __construct(ArticleRepository $repository, EntityManagerInterface $em)
{
$this->repository = $repository;
$this->em = $em;
}
/**
* @Route("/", name="index")
*/
public function index()
{
$articles = $this->repository->findAll();
$postCounts = $this->repository->findPostCountByDate();
return $this->render('articles/index.html.twig', [
'articles' => $articles,
'postCounts' => $postCounts,
]);
}
/**
* @Route("/new", name="new")
*/
public function new(Request $request)
{
$article = new Article();
$form = $this->createForm(ArticleType::class, $article);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->em->persist($article);
$this->em->flush();
$this->addFlash('success', '登録しました');
return $this->redirectToRoute('articles_index');
}
return $this->render('articles/new.html.twig', [
'form' => $form->createView(),
]);
}
}