1
0
Fork 0

Post view and comments

This commit is contained in:
Ethanell 2021-01-14 16:19:35 +01:00
parent ba715dc577
commit ffc9555132
6 changed files with 97 additions and 8 deletions

View file

@ -9,5 +9,4 @@
import './scss/app.scss'; import './scss/app.scss';
// start the Stimulus application // start the Stimulus application
import './bootstrap';
import * as mdb from 'mdb-ui-kit' import * as mdb from 'mdb-ui-kit'

View file

@ -1,2 +1 @@
@import "~bootstrap/scss/bootstrap"; @import '~mdb-ui-kit/src/scss/mdb.free';
@import '~mdb-ui-kit/src/scss/mdb.core';

View file

@ -2,8 +2,12 @@
namespace App\Controller; namespace App\Controller;
use App\Entity\Comment;
use App\Entity\Post; use App\Entity\Post;
use App\Form\CommentType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Annotation\Route;
@ -11,14 +15,35 @@ class PostController extends AbstractController
{ {
/** /**
* @Route("/post/{slug}", name="post") * @Route("/post/{slug}", name="post")
* @param string $slug * @param Request $request
* @param string $slug
* @return Response * @return Response
*/ */
public function index(string $slug): Response public function index(Request $request, string $slug): Response
{ {
$post = $this->getDoctrine()->getRepository(Post::class)->findOneBy(array('slug' => $slug));
$form = $this->formGenerator($post);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$comment = $form->getData();
$comment->setCreatedAt(new \DateTime());
$comment->setValid(true);
$manager = $this->getDoctrine()->getManager();
$manager->persist($comment);
$manager->flush();
$form = $this->formGenerator($post);
}
return $this->render('post/index.html.twig', [ return $this->render('post/index.html.twig', [
'controller_name' => 'PostController', 'controller_name' => 'PostController',
'post' => $this->getDoctrine()->getRepository(Post::class)->findOneBy(array('slug' => $slug)) 'post' => $post,
'form' => $form->createView()
]); ]);
} }
private function formGenerator(Post $post) : FormInterface
{
$comment = new Comment();
$comment->setPost($post);
return $this->createForm(CommentType::class, $comment);
}
} }

30
src/Form/CommentType.php Normal file
View file

@ -0,0 +1,30 @@
<?php
namespace App\Form;
use App\Entity\Comment;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class CommentType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('username', TextType::class, ['attr' => ['class' => 'form-control']])
->add('content', TextareaType::class, ['attr' => ['class' => 'form-control']])
->add('comment', SubmitType::class, ['attr' => ['class' => 'btn btn-primary btn-block']])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Comment::class,
]);
}
}

View file

@ -12,7 +12,7 @@
<!-- Navbar --> <!-- Navbar -->
<nav class="navbar navbar-expand-lg navbar-light bg-light"> <nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid"> <div class="container-fluid">
<a class="navbar-brand" href="{{ path('home') }}m">Blog</a> <a class="navbar-brand" href="{{ path('home') }}">Blog</a>
<button <button
class="navbar-toggler" class="navbar-toggler"

View file

@ -3,5 +3,41 @@
{% block title %}Hello PostController!{% endblock %} {% block title %}Hello PostController!{% endblock %}
{% block body %} {% block body %}
{{ dump(post) }} <div class="container justify-content-center">
<div class="container-fluid">
<h3>{{ post.title }}</h3>
<blockquote class="blockquote">
<p>{{ post.description }}</p>
</blockquote>
<p>{{ post.content }}</p>
</div>
<div class="container-fluid">
<h3>Comments</h3>
{% for comment in post.comments %}
<div class="card">
<div class="card-body">
<h5 class="card-title">{{ comment.username }}</h5>
<p class="card-text">{{ comment.content }}</p>
</div>
<div class="card-footer text-muted">{{ post.createdAt.format('Y-m-d H:i:s') }}</div>
</div>
{% endfor %}
<div class="container-fluid">
<h4>Post a new comment</h4>
{{ form_start(form) }}
{{ form_errors(form) }}
<div class="form-outline">
{{ form_widget(form.username) }}
{{ form_label(form.username, null, {'label_attr': {'class': 'form-label'}}) }}
</div>
<div class="form-outline">
{{ form_widget(form.content) }}
{{ form_label(form.content, null, {'label_attr': {'class': 'form-label'}}) }}
</div>
{{ form_row(form.comment) }}
{{ form_end(form) }}
</div>
</div>
</div>
{% endblock %} {% endblock %}