Skip to content

Commit

Permalink
view products feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Caelly committed Dec 10, 2021
1 parent ce101f4 commit e1b767c
Show file tree
Hide file tree
Showing 17 changed files with 171 additions and 1 deletion.
35 changes: 35 additions & 0 deletions public/assets/css/boutique.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,39 @@
width:100%;
background-color:#DFEAFF;
padding-top:25px;
}

.product-item
{
margin-top:50px;
}

.product-item h5
{
text-transform:uppercase;
font-size:15px;
margin-bottom:1px;
}

.product-item .product-subtitle
{
font-size:14px;
display:block;
margin-bottom:10px;
}

.product-item .product-price
{
font-size:14px;
font-weight:bold;
}

.product-container
{
margin-bottom:150px;
}

.product-page-price
{
font-size:19px;
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions src/Controller/Admin/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Controller\Admin;

use App\Entity\User;
use App\Entity\Product;
use App\Entity\Category;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
Expand Down Expand Up @@ -31,5 +32,6 @@ public function configureMenuItems(): iterable
yield MenuItem::linktoDashboard('Dashboard', 'fa fa-home');
yield MenuItem::linkToCrud('Utilisateur', 'fas fa-user', User::class);
yield MenuItem::linkToCrud('Catégorie', 'fas fa-list', Category::class);
yield MenuItem::linkToCrud('Produit', 'fas fa-tag', Product::class);
}
}
35 changes: 35 additions & 0 deletions src/Controller/Admin/ProductCrudController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Controller\Admin;

use App\Entity\Product;
use EasyCorp\Bundle\EasyAdminBundle\Field\SlugField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use EasyCorp\Bundle\EasyAdminBundle\Field\ImageField;
use EasyCorp\Bundle\EasyAdminBundle\Field\MoneyField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextareaField;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;

class ProductCrudController extends AbstractCrudController
{
public static function getEntityFqcn(): string
{
return Product::class;
}


public function configureFields(string $pageName): iterable
{
return[
TextField::new('name'),
SlugField::new('slug')->setTargetFieldName('name'),
ImageField::new('illustration')->setBasePath('uploads/')->setUploadDir('public/uploads')->setUploadedFileNamePattern('[randomhash].[extension]'),
TextField::new('subtitle'),
TextareaField::new('description'),
MoneyField::new('price')->setCurrency('EUR'),
AssociationField::new('category')
];
}

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

namespace App\Controller;

use App\Entity\Product;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class ProductController extends AbstractController
{
private $entityManager;

public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager=$entityManager;
}

/**
* @Route("/nos-produits", name="products")
*/
public function index(): Response
{
$products = $this->entityManager->getRepository(Product::class)->findAll();
return $this->render('product/index.html.twig', [
'products' => $products
]);
}

/**
* @Route("/produit/{slug}", name="product")
*/
public function show($slug): Response
{
$product = $this->entityManager->getRepository(Product::class)->findOneBySlug($slug);
if(!$product)
{
return $this->redirectToRoute('products');
}
else
{
return $this->render('product/show.html.twig', [
'product' => $product
]);
}
}
}
5 changes: 5 additions & 0 deletions src/Entity/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public function __construct()
$this->products = new ArrayCollection();
}

public function __toString()
{
return $this->getName();
}

public function getId(): ?int
{
return $this->id;
Expand Down
2 changes: 1 addition & 1 deletion templates/base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<div class="collapse navbar-collapse" id="navbarCollapse">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Nos produits</a>
<a class="nav-link" href="{{ path('products') }}">Nos produits</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="#">Qui sommes-nous ?</a>
Expand Down
21 changes: 21 additions & 0 deletions templates/product/index.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{% extends 'base.html.twig' %}

{% block title %}Nos produits | Ma boutique Symfony{% endblock %}

{% block content %}
<h1>Nos produits</h1>
<div class="row product-container">
{% for product in products %}
<div class="col-md-4">
<div class="product-item text-center">
<a href="{{ path('product', {'slug' : product.slug }) }}"><img src="/uploads/{{ product.illustration }}" alt="produit" class="img-fluid"></a>
<h5>{{ product.name }}</h5>
<span class="product-subtitle">{{ product.subtitle }}</span>
<span class="product-price">{{ (product.price / 100)|number_format(2, ',') }}€</span>
</div>
</div>
{% endfor %}
</div>


{% endblock %}
24 changes: 24 additions & 0 deletions templates/product/show.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{% extends 'base.html.twig' %}

{% block title %}{{ product.name }} | Ma boutique Symfony{% endblock %}

{% block content %}

<div class="row">
<div class="col-md-5">
<img src="/uploads/{{ product.illustration }}" alt="{{ product.name }}" class="img-fluid">
</div>
<div class="col-md-7 my-auto">
<h3>{{ product.name }}</h3>
<p>{{ product.subtitle }}</p>
<span class="product-page-price">{{ (product.price / 100)|number_format(2, ',') }}€</span>
<hr>
<p>
{{ product.description }}
</p>
<a href="" class="btn btn-primary">Ajouter au panier</a>
</div>



{% endblock %}

0 comments on commit e1b767c

Please sign in to comment.