Browse Source

Structure controller / route / vue

reduction
Fab 4 years ago
parent
commit
5cf0d8932a
17 changed files with 331 additions and 101 deletions
  1. +79
    -9
      ShopBundle/Controller/AbstractController.php
  2. +27
    -0
      ShopBundle/Controller/CreditConfigController.php
  3. +0
    -43
      ShopBundle/Controller/MerchantController.php
  4. +30
    -0
      ShopBundle/Controller/TaxRateController.php
  5. +29
    -0
      ShopBundle/Form/CreditConfigType.php
  6. +30
    -0
      ShopBundle/Form/TaxRateType.php
  7. +5
    -17
      ShopBundle/Model/CreditConfig.php
  8. +4
    -0
      ShopBundle/Resources/config/services.yaml
  9. +0
    -5
      ShopBundle/Resources/config/shop_routes.yaml
  10. +5
    -0
      ShopBundle/Resources/views/default/_delete_form.html.twig
  11. +4
    -0
      ShopBundle/Resources/views/default/_form.html.twig
  12. +11
    -0
      ShopBundle/Resources/views/default/edit.html.twig
  13. +21
    -0
      ShopBundle/Resources/views/default/index.html.twig
  14. +14
    -0
      ShopBundle/Resources/views/default/show.html.twig
  15. +0
    -7
      ShopBundle/Resources/views/merchant/edit.html.twig
  16. +0
    -20
      ShopBundle/Resources/views/merchant/list.html.twig
  17. +72
    -0
      ShopBundle/Routing/CrudLoader.php

+ 79
- 9
ShopBundle/Controller/AbstractController.php View File

@@ -2,9 +2,13 @@

namespace Lc\ShopBundle\Controller;

use Doctrine\ORM\EntityManagerInterface;
use Lc\ShopBundle\Context\CreditHistoryInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController as SfAbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class AbstractController extends SfAbstractController
abstract class AbstractController extends SfAbstractController
{
/**
* @var \Doctrine\Persistence\ObjectManager
@@ -12,18 +16,84 @@ class AbstractController extends SfAbstractController
*/
protected $em;

/**
* @var object|string
* User currently connected
*/
protected $user;
public $class;

public $type;

public $template;

public $routePrefix;

public $repos;


public function __construct()
public function __construct(EntityManagerInterface $entityManager)
{
$this->em = $entityManager;
$metadata = $this->em->getClassMetadata($this->class);
$this->class = $metadata->getName();
$this->repo = $this->em->getRepository($this->class);

$this->em = $this->getDoctrine()->getManager();
$this->user = $this->get('security.token_storage')->getToken()->getUser();
}

public function indexAction(): Response
{
$repo = $this->em->getRepository($this->class);
return $this->render('LcShopBundle:' . $this->template . ':index.html.twig', [
'entities' => $repo->findAll(),
'routePrefix'=> $this->routePrefix
]);
}

public function showAction($id): Response
{
$enity = $this->repo->find($id);

return $this->render('LcShopBundle:'.$this->template.':show.html.twig', [
'entity' => $enity,
'routePrefix'=> $this->routePrefix

]);
}
public function editAction(Request $request, $id): Response
{

if ($id == 'new') {
$entity = new $this->class;
} else {
$repo = $this->em->getRepository($this->class);
$entity = $repo->find($id);
}
$form = $this->createForm($this->type, $entity);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$this->em->persist($entity);
$this->getDoctrine()->getManager()->flush();

return $this->redirectToRoute($this->routePrefix . '_index');
}


return $this->render('LcShopBundle:' . $this->template . ':edit.html.twig', [
'entity' => $entity,
'form' => $form->createView(),
'routePrefix'=> $this->routePrefix
]);
}


public function deleteAction(Request $request, $id): Response
{
$enity = $this->repo->find($id);

if ($this->isCsrfTokenValid('delete'.$enity->getId(), $request->request->get('_token'))) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($enity);
$entityManager->flush();
}

return $this->redirectToRoute($this->routePrefix.'_index');
}

}

+ 27
- 0
ShopBundle/Controller/CreditConfigController.php View File

@@ -0,0 +1,27 @@
<?php

namespace Lc\ShopBundle\Controller;


use Doctrine\ORM\EntityManagerInterface;
use Lc\ShopBundle\Context\CreditConfigInterface;
use Lc\ShopBundle\Form\CreditConfigType;

class CreditConfigController extends AbstractController
{

protected $repo;

public function __construct(EntityManagerInterface $entityManager)
{
//Cette valeur sera récrite dans le AbstractController pour qu'elle pointe vers l'entité et non plus l'interface
$this->class = CreditConfigInterface::class;
$this->type = CreditConfigType::class;
$this->template = "default";
$this->routePrefix = "lc_shop_credit_config";

parent::__construct($entityManager);

}

}

+ 0
- 43
ShopBundle/Controller/MerchantController.php View File

@@ -1,43 +0,0 @@
<?php

namespace Lc\ShopBundle\Controller;

use App\Entity\Merchant;
use Lc\ShopBundle\Context\MerchantInterface;

class MerchantController extends AbstractController
{
protected $repo;
public function __construct()
{
$this->repo = $this->em->getRepository(MerchantInterface::class);

}


public function edit(){
$merchant = new Merchant();
$merchant->setCreatedBy($this->user);
$merchant->setUpdatedBy($this->user);

$this->em->persist($merchant);
$this->em->flush();
die();

$merchant = $this->repo->find($id);
dump($merchant);

return $this->render('@LcShop/merchant/edit.html.twig', [

]);
}


public function list(){
return $this->render('merchant/index.html.twig', [
'controller_name' => 'MerchantController',
]);
}


}

+ 30
- 0
ShopBundle/Controller/TaxRateController.php View File

@@ -0,0 +1,30 @@
<?php

namespace Lc\ShopBundle\Controller;

use Doctrine\ORM\EntityManagerInterface;
use Lc\ShopBundle\Context\TaxRateInterface;
use Lc\ShopBundle\Form\TaxRateType;

class TaxRateController extends AbstractController
{

protected $repo;

public function __construct(EntityManagerInterface $entityManager)
{
//Cette valeur sera récrite dans le AbstractController pour qu'elle pointe vers l'entité et non plus l'interface
$this->class = TaxRateInterface::class;
$this->type = TaxRateType::class;
$this->template = "default";
$this->routePrefix = "lc_shop_tax_rate";

parent::__construct($entityManager);

}

public function newBeforeFlush(){

}

}

+ 29
- 0
ShopBundle/Form/CreditConfigType.php View File

@@ -0,0 +1,29 @@
<?php

namespace Lc\ShopBundle\Form;

use Lc\ShopBundle\Context\CreditConfigInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class CreditConfigType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('active')
->add('limitAmount')
->add('limitReminder')
->add('behavior')
->add('processOrderCheckedDefault')
;
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => CreditConfigInterface::class,
]);
}
}

+ 30
- 0
ShopBundle/Form/TaxRateType.php View File

@@ -0,0 +1,30 @@
<?php

namespace Lc\ShopBundle\Form;

use Lc\ShopBundle\Context\TaxRateInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class TaxRateType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title')
->add('value')
->add('createdAt')
->add('updatedAt')
->add('createdBy')
->add('updatedBy')
;
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => TaxRateInterface::class,
]);
}
}

+ 5
- 17
ShopBundle/Model/CreditConfig.php View File

@@ -9,42 +9,30 @@ use Doctrine\ORM\Mapping as ORM;
*/
abstract class CreditConfig
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;

/**
* @ORM\Column(type="boolean")
*/
private $active;
protected $active;

/**
* @ORM\Column(type="float", nullable=true)
*/
private $limitAmount;
protected $limitAmount;

/**
* @ORM\Column(type="float", nullable=true)
*/
private $limitReminder;
protected $limitReminder;

/**
* @ORM\Column(type="string", length=31, nullable=true)
*/
private $behavior;
protected $behavior;

/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $processOrderCheckedDefault;

public function getId(): ?int
{
return $this->id;
}
protected $processOrderCheckedDefault;

public function getActive(): ?bool
{

+ 4
- 0
ShopBundle/Resources/config/services.yaml View File

@@ -0,0 +1,4 @@
services:
# ...
Lc\ShopBundle\Routing\ExtraLoader:
tags: [routing.loader]

+ 0
- 5
ShopBundle/Resources/config/shop_routes.yaml View File

@@ -1,6 +1 @@

lc_merchant_list:
path: /merchant/list
controller: LcShopBundle:Merchant:list



+ 5
- 0
ShopBundle/Resources/views/default/_delete_form.html.twig View File

@@ -0,0 +1,5 @@
<form method="post" action="{{ path(routePrefix~'_delete', {'id': entity.id}) }}" onsubmit="return confirm('Are you sure you want to delete this item?');">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token('delete' ~ entity.id) }}">
<button class="btn">Delete</button>
</form>

+ 4
- 0
ShopBundle/Resources/views/default/_form.html.twig View File

@@ -0,0 +1,4 @@
{{ form_start(form) }}
{{ form_widget(form) }}
<button class="btn">{{ button_label|default('Save') }}</button>
{{ form_end(form) }}

+ 11
- 0
ShopBundle/Resources/views/default/edit.html.twig View File

@@ -0,0 +1,11 @@
{% extends 'base.html.twig' %}

{% block title %}Edit Entity{% endblock %}

{% block body %}
<h1>Edit CreditConfig</h1>

{{ include('@LcShop/default/_form.html.twig', {'button_label': 'Update'}) }}


{% endblock %}

+ 21
- 0
ShopBundle/Resources/views/default/index.html.twig View File

@@ -0,0 +1,21 @@
{% extends 'base.html.twig' %}

{% block title %}CreditConfig index{% endblock %}

{% block body %}
<h1>liste entités par défaut </h1>

<ul>
{% for entity in entities %}
<li>
{{ dump(entity) }}
<a href="{{ path(routePrefix~'_edit', {"id" : entity.id}) }}">Editer cette entité</a>
<a href="{{ path(routePrefix~'_show', {"id" : entity.id}) }}">Afficher cette entité</a>
{{ include('@LcShop/default/_delete_form.html.twig') }}
</li>
{% else %}
Aucun enregistrement
{% endfor %}
</ul>
<p> <a href="{{ path(routePrefix~'_edit', {"id" : 'new'}) }}">Ajouter une nouvelle entité</a></p>
{% endblock %}

+ 14
- 0
ShopBundle/Resources/views/default/show.html.twig View File

@@ -0,0 +1,14 @@
{% extends 'base.html.twig' %}

{% block title %}CreditConfig{% endblock %}

{% block body %}
<h1>My entity</h1>

{{ dump(entity) }}
<a href="{{ path(routePrefix~'_index') }}">back to list</a>

<a href="{{ path(routePrefix~'_edit', {'id': entity.id}) }}">edit</a>

{{ include('@LcShop/default/_delete_form.html.twig') }}
{% endblock %}

+ 0
- 7
ShopBundle/Resources/views/merchant/edit.html.twig View File

@@ -1,7 +0,0 @@
{% extends 'base.html.twig' %}

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

{% block body %}
YEAH MEC!!!!
{% endblock %}

+ 0
- 20
ShopBundle/Resources/views/merchant/list.html.twig View File

@@ -1,20 +0,0 @@
{% extends 'base.html.twig' %}

{% block title %}Hello MerchantController!{% 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>Hello {{ controller_name }}! ✅</h1>

This friendly message is coming from:
<ul>
<li>Your controller at <code><a href="{{ '/home/fab/Documents/Pro/www/pdl/sandbox/src/Controller/MerchantController.php'|file_link(0) }}">src/Controller/MerchantController.php</a></code></li>
<li>Your template at <code><a href="{{ '/home/fab/Documents/Pro/www/pdl/sandbox/templates/merchant/index.html.twig'|file_link(0) }}">templates/merchant/index.html.twig</a></code></li>
</ul>
</div>
{% endblock %}

+ 72
- 0
ShopBundle/Routing/CrudLoader.php View File

@@ -0,0 +1,72 @@
<?php

namespace Lc\ShopBundle\Routing;

use Lc\ShopBundle\Model\Address;
use Symfony\Component\Config\Loader\Loader;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

class CrudLoader extends Loader
{
private $isLoaded = false;

public function load($resource, $type = null)
{
if (true === $this->isLoaded) {
throw new \RuntimeException('Do not add the "extra" loader twice');
}

$routes = new RouteCollection();
$lcShopRoutes = array(
'address' => "Address",
"cart" => "Cart",
"credit_config" => "CreditConfig",
"document_nelivery_note" => "DocumentDeliveryNote",
"document_quotation" => "DocumentQuotation",
"document_invoice" => "DocumentInvoice",
"merchant" => "Merchant",
"merchant_config" => "MerchantConfig",
/*"order"=>"Order"*/
"point_sale" => "PointSale",
"tax_rate" => "TaxRate",
"product" => "Products"
);
$actions = [
'index' => 'indexAction',
'edit' => 'editAction',
'delete' => 'deleteAction',
'show' => 'showAction'
];

foreach ($lcShopRoutes as $entity => $controller) {
foreach ($actions as $actionName => $action) {
if ($actionName == 'edit' || $actionName == 'delete' || $actionName == "show") {
$path = '/' . $entity . '/' . $actionName . '/{id}';
} else {
$path = '/' . $entity . '/' . $actionName;
}
$defaults = [
'_controller' => 'Lc\ShopBundle\Controller\\' . $controller . 'Controller::' . $action
];
$requirements = [
'parameter' => '\d+',
];
$route = new Route($path, $defaults, $requirements);

$routeName = 'lc_shop_' . $entity . '_' . $actionName;

$routes->add($routeName, $route);

}
}
$this->isLoaded = true;
return $routes;
}


public function supports($resource, $type = null)
{
return 'crud' === $type;
}
}

Loading…
Cancel
Save