diff --git a/composer.json b/composer.json index 065e6e9..f8210d1 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "php": "^7.1.3", "ext-ctype": "*", "ext-iconv": "*", - "sensio/framework-extra-bundle": "^5.1", + "sensio/framework-extra-bundle": "^5.3", "symfony/asset": "4.2.*", "symfony/console": "4.2.*", "symfony/dotenv": "4.2.*", diff --git a/composer.lock b/composer.lock index 5aef7f2..34e0c04 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "8318eee38ca02b3401669a5f10717954", + "content-hash": "43e52d89c1b69827c7232e3b3fa7c085", "packages": [ { "name": "doctrine/annotations", diff --git a/src/Controller/BlogContorllerController.php b/src/Controller/BlogContorllerController.php deleted file mode 100644 index 0a9935b..0000000 --- a/src/Controller/BlogContorllerController.php +++ /dev/null @@ -1,19 +0,0 @@ -render('blog_contorller/index.html.twig', [ - 'controller_name' => 'BlogContorllerController', - ]); - } -} diff --git a/src/Controller/BlogController.php b/src/Controller/BlogController.php new file mode 100644 index 0000000..271a4eb --- /dev/null +++ b/src/Controller/BlogController.php @@ -0,0 +1,125 @@ +getDoctrine()->getRepository(Blog::class); + $blogs = $repository->findAll(); + + return $this->render('blog/index.html.twig', [ + 'blogs' => $blogs, + ]); + } + + /** + * @Route("/create", name="create") + @Method("GET") + */ + public function create() + { + return $this->render('create.html.twig'); + } + + /** + * @Route("/submit", name="submit") + @Method("POST") + */ + public function submit(Request $request) + { + $blog=new Blog(); + $title=$request->request->get('title'); + $author=$request->request->get('author'); + $date=$request->request->get('date'); + $content=$request->request->get('content'); + + $blog->setTitle($title); + $blog->setAuthor($author); + $blog->setContent($content); + + + + $em=$this->getDoctrine()->getManager(); + $em->persist($blog); + $em->flush(); + + return $this->redirectToRoute('create'); + + } + + /** + * @Route("/show/{id}", name="show") + @Method("GET") + */ + public function show($id) + { + $repository = $this->getDoctrine()->getRepository(Blog::class); + $blog = $repository->find($id); + return $this->render('show.html.twig',[ + 'blog'=>$blog + ]); + } + + /** + * @Route("/edit/{id}", name="edit") + @Method("GET") + */ + public function edit($id) + { + $blog = $this->getDoctrine()->getRepository(Blog::class)->find($id); + + return $this->render('edit.html.twig',[ + 'blog'=>$blog + ]); + } + + /** + * @Route("/edit-submit/{id}", name="edit-submit") + @Method("PUT") + */ + public function editSubmit(Request $request, $id) + { + $blog = $this->getDoctrine()->getRepository(Blog::class)->find($id); + $title=$request->request->get('title'); + $author=$request->request->get('author'); + $date=$request->request->get('date'); + $content=$request->request->get('content'); + + $blog->setTitle($title); + $blog->setAuthor($author); + $blog->setContent($content); + + $em=$this->getDoctrine()->getManager(); + + $em->flush(); + + return $this->redirectToRoute('blog'); + } + + /** + * @Route("delete/{id}", name="delete") + * @Method("DELETE") + */ + public function delete(Request $request, $id) + { + $blog = $this->getDoctrine()->getRepository(Blog::class)->find($id); + + $em=$this->getDoctrine()->getManager(); + $em->remove($blog); + $em->flush(); + + return $this->redirectToRoute('blog'); + } +} diff --git a/src/Entity/Blog.php b/src/Entity/Blog.php index 192b34a..8223686 100644 --- a/src/Entity/Blog.php +++ b/src/Entity/Blog.php @@ -17,27 +17,64 @@ class Blog */ private $id; + public function getId() + { + return $this->id; + } + /** * @ORM\Column(type="string", length=255) */ private $title; + public function setTitle($title) + { + $this->title = $title; + + return $this; + } + + public function getTitle() + { + return $this->title; + } + /** * @ORM\Column(type="string", length=255) */ private $author; + public function getAuthor() + { + return $this->author; + } + + public function setAuthor($author) + { + $this->author = $author; + + return $this; + } + /** * @ORM\Column(type="text") */ private $content; - /** - * @ORM\Column(type="datetime") - */ + public function getContent() + { + return $this->content; + } + + public function setContent($content) + { + $this->content = $content; + + return $this; + } + - private $date; } diff --git a/src/Migrations/Version20190504233047.php b/src/Migrations/Version20190504233047.php new file mode 100644 index 0000000..79a493f --- /dev/null +++ b/src/Migrations/Version20190504233047.php @@ -0,0 +1,35 @@ +abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.'); + + $this->addSql('CREATE TABLE blogs (id INT AUTO_INCREMENT NOT NULL, title VARCHAR(255) NOT NULL, author VARCHAR(255) NOT NULL, content LONGTEXT NOT NULL, date DATE NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB'); + } + + public function down(Schema $schema) : void + { + // this down() migration is auto-generated, please modify it to your needs + $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.'); + + $this->addSql('DROP TABLE blogs'); + } +} diff --git a/templates/base.html.twig b/templates/base.html.twig index 043f42d..9aee648 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -3,10 +3,15 @@ {% block title %}Welcome!{% endblock %} + + {% block stylesheets %}{% endblock %} {% block body %}{% endblock %} {% block javascripts %}{% endblock %} + + + diff --git a/templates/blog/index.html.twig b/templates/blog/index.html.twig new file mode 100644 index 0000000..eaee9e6 --- /dev/null +++ b/templates/blog/index.html.twig @@ -0,0 +1,45 @@ +{% extends 'base.html.twig' %} + +{% block title %}Hello BlogController!{% endblock %} + +{% block body %} + + +
+

All Blogs ✅

+ + + + + Title + + + Author + + + Settings + + + + {% for blog in blogs %} + + + + {% endfor %} + +
+ {{blog.title()}} + {{blog.author}} + Show + Edit +
+ + + +
+ +
+{% endblock %} diff --git a/templates/create.html.twig b/templates/create.html.twig new file mode 100644 index 0000000..6f29acd --- /dev/null +++ b/templates/create.html.twig @@ -0,0 +1,35 @@ +{% extends 'base.html.twig' %} + +{% block title %}Hello BlogContorllerController!{% endblock %} + +{% block body %} + + +
+

Create Blog ✅

+ + +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ + + +
+{% endblock %} diff --git a/templates/edit.html.twig b/templates/edit.html.twig new file mode 100644 index 0000000..4a54a33 --- /dev/null +++ b/templates/edit.html.twig @@ -0,0 +1,37 @@ +{% extends 'base.html.twig' %} + +{% block title %}Hello BlogContorllerController!{% endblock %} + +{% block body %} + + +
+

Create Blog ✅

+ +
+ + +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ + +
+
+{% endblock %} diff --git a/templates/show.html.twig b/templates/show.html.twig new file mode 100644 index 0000000..90330fe --- /dev/null +++ b/templates/show.html.twig @@ -0,0 +1,24 @@ +{% extends 'base.html.twig' %} + +{% block title %}Hello BlogController!{% endblock %} + +{% block body %} + + +
+

Single Blog

+ + +

{{blog.title}}

+

{{blog.author}}

+

{{blog.content}}

+ + + + + +
+{% endblock %}