-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathArticlesController.php
More file actions
64 lines (55 loc) · 1.71 KB
/
ArticlesController.php
File metadata and controls
64 lines (55 loc) · 1.71 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
<?php
namespace App\Controller;
use App\Entity\Article;
use App\Form\ArticleType;
use App\Service\ArticleService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Throwable;
class ArticlesController extends AbstractController
{
/** @var ArticleService */
private $articleService;
/**
* ArticlesController constructor.
* @param ArticleService $articleService
*/
public function __construct(ArticleService $articleService)
{
$this->articleService = $articleService;
}
/**
* @Route("/articles", name="articles")
*/
public function index()
{
$articles = $this->articleService->getList();
return $this->render('articles/index.html.twig', [
'articles' => $articles,
]);
}
/**
* @Route("/articles/new", name="articles_new")
* @param Request $request
* @return RedirectResponse|Response
* @throws Throwable
*/
public function new(Request $request)
{
$article = new Article();
$form = $this->createForm(ArticleType::class, $article);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$article = $form->getData();
$this->articleService->add($article);
$this->addFlash('success', '登録しました');
return $this->redirectToRoute('articles');
}
return $this->render('articles/new.html.twig', [
'form' => $form->createView(),
]);
}
}