Skip to content

Commit

Permalink
CRUD
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdzafo committed May 5, 2019
1 parent 6c9adf4 commit 698bd44
Show file tree
Hide file tree
Showing 11 changed files with 349 additions and 25 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.*",
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 0 additions & 19 deletions src/Controller/BlogContorllerController.php

This file was deleted.

125 changes: 125 additions & 0 deletions src/Controller/BlogController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use App\Entity\Blog;
use Doctrine\ORM\EntityManagerInterface;

class BlogController extends AbstractController
{
/**
* @Route("/blog", name="blog")
*/
public function index()
{
$repository = $this->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');
}
}
45 changes: 41 additions & 4 deletions src/Entity/Blog.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}
35 changes: 35 additions & 0 deletions src/Migrations/Version20190504233047.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20190504233047 extends AbstractMigration
{
public function getDescription() : string
{
return '';
}

public function up(Schema $schema) : void
{
// this up() 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('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');
}
}
5 changes: 5 additions & 0 deletions templates/base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
<head>
<meta charset="UTF-8">
<title>{% block title %}Welcome!{% endblock %}</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

{% block stylesheets %}{% endblock %}
</head>
<body>
{% block body %}{% endblock %}
{% block javascripts %}{% endblock %}
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>
45 changes: 45 additions & 0 deletions templates/blog/index.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{% extends 'base.html.twig' %}

{% block title %}Hello BlogController!{% endblock %}

{% block body %}
<style>
.example-wrapper { margin: 1em auto; max-width: 800px; width: 95%; font: 18px/1.5 sans-serif; }
.example-wrapper code { background: #F5F5F5; padding: 2px 6px; }
</style>

<div class="example-wrapper">
<h1>All Blogs ✅</h1>

<table>
<thead>
<tr>
Title
</tr>
<tr>
Author
</tr>
<tr>
Settings
</tr>
</thead>
<tbody>
{% for blog in blogs %}
<td>
{{blog.title()}}
</td>
<td>{{blog.author}}</td>
<td>
<a href="/show/{{blog.id}}" class="btn btn-primary">Show</a>
<a href="/edit/{{blog.id}}" class="btn btn-info">Edit</a>
<form class="" action="{{path('delete',{ 'id' : blog.id })}}" method="post">
<input type='hidden' name='_method' value='DELETE'>
<button type="submit" name="button" class="btn btn-danger">Delete</button>

</td>
{% endfor %}
</tbody>
</table>

</div>
{% endblock %}
35 changes: 35 additions & 0 deletions templates/create.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{% extends 'base.html.twig' %}

{% block title %}Hello BlogContorllerController!{% endblock %}

{% block body %}


<div class="example-wrapper">
<h1>Create Blog ✅</h1>

<form class="" action="{{path('submit')}}" method="post">
<div class="form-group">
<label for="name" class="control-label">Title</label>
<input type="text" class="form-control" name="title">
</div>

<div class="form-group">
<label for="name" class="control-label">Author</label>
<input type="text" class="form-control" name="author">
</div>

<div class="form-group">
<label for="name" class="control-label">Date</label>
<input type="date" class="form-control" name="date">
</div>

<div class="form-group">
<label for="name" class="control-label">Content</label>
<textarea name="content" rows="8" class="form-control"></textarea>
</div>

<button type="submit" name="button">Save</button>
</form>
</div>
{% endblock %}
Loading

0 comments on commit 698bd44

Please sign in to comment.