Last active
December 31, 2015 18:59
-
-
Save amenophis/8030990 to your computer and use it in GitHub Desktop.
[Symfony] CRUD avec SyliusResourceBundle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{ form_widget(form) }} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Symfony\Component\HttpKernel\Kernel; | |
use Symfony\Component\Config\Loader\LoaderInterface; | |
class AppKernel extends Kernel | |
{ | |
public function registerBundles() | |
{ | |
$bundles = array( | |
// ... | |
new FOS\RestBundle\FOSRestBundle(), | |
new JMS\SerializerBundle\JMSSerializerBundle($this), | |
new Sylius\Bundle\ResourceBundle\SyliusResourceBundle(), | |
new WhiteOctober\PagerfantaBundle\WhiteOctoberPagerfantaBundle(), | |
// ... | |
); | |
if (in_array($this->getEnvironment(), array('dev', 'test'))) { | |
$bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle(); | |
$bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle(); | |
$bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle(); | |
} | |
return $bundles; | |
} | |
public function registerContainerConfiguration(LoaderInterface $loader) | |
{ | |
$loader->load(__DIR__.'/config/config_'.$this->getEnvironment().'.yml'); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... | |
sylius_resource: | |
resources: | |
tj.user: | |
driver: doctrine/orm | |
templates: TataJeremSyliusCRUDBundle:User | |
classes: | |
model: TataJerem\Bundle\SyliusCRUDBundle\Entity\User | |
controller: TataJerem\Bundle\SyliusCRUDBundle\Controller\UserController | |
// ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends "TataJeremSyliusCRUDBundle::_base.html.twig" %} | |
{% block page_title %}{{ parent() }} - Ajouter un utilisateur{% endblock page_title %} | |
{% block body %} | |
<h1>Ajouter un utilisateur</h1> | |
<form action="{{ path('tj_user_create') }}" method="post" autocomplete="off"> | |
{% include "TataJeremSyliusCRUDBundle:User:_form.html.twig" %} | |
<div class="pull-left"> | |
<a href="{{ path('tj_user_index') }}" class="btn btn-default"> | |
<span class="glyphicon glyphicon-arrow-left"></span> Retour | |
</a> | |
</div> | |
<div class="pull-right"> | |
<button type="submit" class="btn btn-primary"> | |
<span class="glyphicon glyphicon-plus"></span> Ajouter | |
</button> | |
</div> | |
</form> | |
{% endblock body %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends "TataJeremSyliusCRUDBundle::_base.html.twig" %} | |
{% block page_title %}{{ parent() }} - Liste des utilisateurs{% endblock page_title %} | |
{% block body %} | |
<h1>Liste des utilisateurs</h1> | |
<table class="table table-striped"> | |
<thead> | |
<tr> | |
<th style="width: 1px;">{{ sylius_resource_sort('id', '#') }}</th> | |
<th>{{ sylius_resource_sort('username', 'Nom d\'utilisateur') }}</th> | |
<th>{{ sylius_resource_sort('email', 'Adresse e-mail') }}</th> | |
<th style="width: 1px;"></th> | |
</tr> | |
</thead> | |
<tbody> | |
{% for user in users %} | |
<tr> | |
<td>{{ user.id }}</td> | |
<td><a href="{{ path('tj_user_show', { 'id': user.id }) }}">{{ user.username }}</a></td> | |
<td>{{ user.email }}</td> | |
<td> | |
<button type="submit" form="user_{{ user.id }}" class="btn btn-danger"><span class="glyphicon glyphicon-remove"></span> Supprimer</button> | |
<form id="user_{{ user.id }}" action="{{ path('tj_user_delete', { 'id': user.id }) }}" method="post" onsubmit="return confirm('Supprimer ?')"> | |
<input type="hidden" name="_method" value="DELETE" /> | |
<input type="hidden" name="confirmed" value="1" /> | |
</form> | |
</td> | |
</tr> | |
{% endfor %} | |
</tbody> | |
</table> | |
<div class="pull-right"> | |
<a href="{{ path('tj_user_create') }}" class="btn btn-primary"> | |
<span class="glyphicon glyphicon-plus"></span> Ajouter | |
</a> | |
</div> | |
{% endblock body %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tj_user_index: | |
pattern: /user | |
methods: [GET] | |
defaults: | |
_controller: tj.controller.user:indexAction | |
_sylius: | |
sortable: true | |
tj_user_show: | |
pattern: /user/{id} | |
methods: [GET] | |
requirements: | |
id: \d+ | |
defaults: | |
_controller: tj.controller.user:showAction | |
tj_user_create: | |
pattern: /user/new | |
methods: [GET, POST] | |
defaults: | |
_controller: tj.controller.user:createAction | |
tj_user_update: | |
pattern: /user/{id}/edit | |
methods: [GET, PUT] | |
defaults: | |
_controller: tj.controller.user:updateAction | |
tj_user_delete: | |
pattern: /user/{id} | |
methods: [DELETE] | |
defaults: | |
_controller: tj.controller.user:deleteAction |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" ?> | |
<container xmlns="http://symfony.com/schema/dic/services" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | |
<parameters> | |
<parameter key="tata_jerem_sylius_crud.form.user.class">TataJerem\Bundle\SyliusCRUDBundle\Form\UserType</parameter> | |
</parameters> | |
<services> | |
<service id="tata_jerem_sylius_crud.form.user" class="%tata_jerem_sylius_crud.form.user.class%"> | |
<tag name="form.type" alias="tj_user" /> | |
</service> | |
</services> | |
</container> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends "TataJeremSyliusCRUDBundle::_base.html.twig" %} | |
{% block page_title %}{{ parent() }} - Fiche de {{ user.username }}{% endblock page_title %} | |
{% block body %} | |
<h1>{{ user.username }}</h1> | |
<table class="table table-stripped"> | |
<tbody> | |
<tr> | |
<th>#</th> | |
<td>{{ user.id }}</td> | |
</tr> | |
<tr> | |
<th>Nom d'utilisateur</th> | |
<td>{{ user.username }}</td> | |
</tr> | |
<tr> | |
<th>Adresse e-mail</th> | |
<td>{{ user.email }}</td> | |
</tr> | |
</tbody> | |
</table> | |
<div class="row"> | |
<div class="pull-left"> | |
<a class="btn btn-default" href="{{ path('tj_user_index') }}"><span class="glyphicon glyphicon-arrow-left"></span> Retour à la liste</a> | |
</div> | |
<div class="pull-right"> | |
<a class="btn btn-primary" href="{{ path('tj_user_update', { 'id': user.id }) }}"><span class="glyphicon glyphicon-pencil"></span> Modifier</a> | |
</div> | |
</div> | |
{% endblock body %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends "TataJeremSyliusCRUDBundle::_base.html.twig" %} | |
{% block page_title %}{{ parent() }} - Modifier {{ user.username }}{% endblock page_title %} | |
{% block body %} | |
<h1>Modifier {{ user.username }}</h1> | |
<form action="{{ path('tj_user_update', { 'id': user.id }) }}" method="post" novalidate autocomplete="off"> | |
<input type="hidden" name="_method" value="PUT" /> | |
{% include "TataJeremSyliusCRUDBundle:User:_form.html.twig" %} | |
<div class="pull-left"> | |
<a href="{{ path('tj_user_show', { 'id': user.id }) }}" class="btn btn-default"> | |
<span class="glyphicon glyphicon-arrow-left"></span> Retour | |
</a> | |
</div> | |
<div class="pull-right"> | |
<button type="submit" class="btn btn-primary"> | |
<span class="glyphicon glyphicon-floppy-disk"></span> Valider | |
</button> | |
</div> | |
</form> | |
{% endblock body %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace TataJerem\Bundle\SyliusCRUDBundle\Controller; | |
use FOS\UserBundle\Model\UserManagerInterface; | |
use Sylius\Bundle\ResourceBundle\Controller\ResourceController; | |
use Symfony\Component\HttpFoundation\Request; | |
class UserController extends ResourceController | |
{ | |
/** | |
* @return UserManagerInterface | |
*/ | |
public function getUserManager() | |
{ | |
return $this->get('fos_user.user_manager'); | |
} | |
public function createNew() | |
{ | |
return $this->getUserManager()->createUser(); | |
} | |
public function update($resource) | |
{ | |
$this->getUserManager()->updateUser($resource, false); | |
return parent::update($resource); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace TataJerem\Bundle\SyliusCRUDBundle\Form; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\OptionsResolver\OptionsResolverInterface; | |
class UserType extends AbstractType | |
{ | |
/** | |
* @param FormBuilderInterface $builder | |
* @param array $options | |
*/ | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$builder | |
->add('username') | |
->add('email') | |
->add('plain_password', 'repeated', array( | |
'type' => 'password', | |
'invalid_message' => 'Les mots de passe doivent correspondre', | |
'options' => array('required' => true), | |
'first_options' => array('label' => 'Mot de passe'), | |
'second_options' => array('label' => 'Mot de passe (confirmation)'), | |
)) | |
->add('enabled') | |
; | |
} | |
/** | |
* @param OptionsResolverInterface $resolver | |
*/ | |
public function setDefaultOptions(OptionsResolverInterface $resolver) | |
{ | |
$resolver->setDefaults(array( | |
'data_class' => 'TataJerem\Bundle\SyliusCRUDBundle\Entity\User' | |
)); | |
} | |
/** | |
* @return string | |
*/ | |
public function getName() | |
{ | |
return 'tj_user'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment