API: controllers + form + view
This commit is contained in:
parent
2d49e99002
commit
3a85b7c12b
37
src/AppBundle/Controller/Api/BookController.php
Normal file
37
src/AppBundle/Controller/Api/BookController.php
Normal file
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
|
||||
namespace AppBundle\Controller\Api;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
|
||||
use FOS\RestBundle\Controller\Annotations as Rest;
|
||||
use FOS\RestBundle\Controller\FOSRestController;
|
||||
use AppBundle\Entity\Book;
|
||||
|
||||
/**
|
||||
* class BookController.
|
||||
*
|
||||
* @author Simon Vieille <simon@deblan.fr>
|
||||
*/
|
||||
class BookController extends FOSRestController
|
||||
{
|
||||
/**
|
||||
* Get books.
|
||||
*
|
||||
* @Rest\View
|
||||
*
|
||||
* @ApiDoc(
|
||||
* description="Get books.",
|
||||
* )
|
||||
*
|
||||
* @param Request $request
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getBooksAction(Request $request)
|
||||
{
|
||||
return $this->getDoctrine()
|
||||
->getRepository(Book::class)
|
||||
->findAll();
|
||||
}
|
||||
}
|
90
src/AppBundle/Controller/Api/CategoryController.php
Normal file
90
src/AppBundle/Controller/Api/CategoryController.php
Normal file
|
@ -0,0 +1,90 @@
|
|||
<?php
|
||||
|
||||
namespace AppBundle\Controller\Api;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
|
||||
use FOS\RestBundle\Controller\Annotations as Rest;
|
||||
use FOS\RestBundle\View\View;
|
||||
use FOS\RestBundle\Controller\FOSRestController;
|
||||
use AppBundle\Entity\Category;
|
||||
use AppBundle\Form\CategoryType;
|
||||
|
||||
/**
|
||||
* class CategoryController.
|
||||
*
|
||||
* @author Simon Vieille <simon@deblan.fr>
|
||||
*/
|
||||
class CategoryController extends FOSRestController
|
||||
{
|
||||
/**
|
||||
* Get categories.
|
||||
*
|
||||
* @Rest\View
|
||||
*
|
||||
* @ApiDoc(
|
||||
* description="Get categories.",
|
||||
* )
|
||||
*
|
||||
* @param Request $request
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getCategoriesAction(Request $request)
|
||||
{
|
||||
return $this->getDoctrine()
|
||||
->getRepository(Category::class)
|
||||
->findAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a category.
|
||||
*
|
||||
* @Rest\View
|
||||
*
|
||||
* @ApiDoc(
|
||||
* description="Get a category.",
|
||||
* )
|
||||
*
|
||||
* @param Request $request
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getCategoryction(Request $request, Category $category)
|
||||
{
|
||||
return $category;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a category.
|
||||
*
|
||||
* @Rest\View
|
||||
* @Rest\Post("/categories/new")
|
||||
* @ApiDoc(
|
||||
* description="Create a category.",
|
||||
* input={
|
||||
* "class"="AppBundle\Form\CategoryType"
|
||||
* }
|
||||
* )
|
||||
*
|
||||
* @param Request $request
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function postCategoryNewAction(Request $request)
|
||||
{
|
||||
$object = new Category();
|
||||
$form = $this->createForm(CategoryType::class, $object);
|
||||
$form->handleRequest($request);
|
||||
|
||||
if (!$form->isSubmitted() || !$form->isValid()) {
|
||||
return $this->view($form->getErrors(true), 400);
|
||||
}
|
||||
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
$em->persist($form->getData());
|
||||
$em->flush();
|
||||
|
||||
return ['status' => true];
|
||||
}
|
||||
}
|
36
src/AppBundle/Controller/CategoryController.php
Normal file
36
src/AppBundle/Controller/CategoryController.php
Normal file
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
namespace AppBundle\Controller;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Trinity\Bundle\ContentManagerBundle\Controller\PageController;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use AppBundle\Entity\Category;
|
||||
use AppBundle\Form\CategoryType;
|
||||
|
||||
/**
|
||||
* class CategoryController.
|
||||
*
|
||||
* @author Simon Vieille <simon@deblan.fr>
|
||||
*/
|
||||
class CategoryController extends Controller
|
||||
{
|
||||
/**
|
||||
* Categories page.
|
||||
*
|
||||
* @Template()
|
||||
* @Route("/", name="example")
|
||||
*
|
||||
* @param Request $request
|
||||
*
|
||||
* @return \Symfony\Bundle\FrameworkBundle\Controller\Response
|
||||
*/
|
||||
public function indexAction(Request $request)
|
||||
{
|
||||
return [
|
||||
'form' => $this->createForm(CategoryType::class, new Category())->createView(),
|
||||
];
|
||||
}
|
||||
}
|
50
src/AppBundle/Form/CategoryType.php
Normal file
50
src/AppBundle/Form/CategoryType.php
Normal file
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
namespace AppBundle\Form;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use Symfony\Component\Validator\Constraints\NotBlank;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
|
||||
class CategoryType extends AbstractType
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$builder->add(
|
||||
'name',
|
||||
TextType::class,
|
||||
[
|
||||
'label' => 'Nom',
|
||||
'required' => false, // html5 validation disable for the example
|
||||
'constraints' => [
|
||||
new NotBlank(),
|
||||
],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
$resolver->setDefaults([
|
||||
'data_class' => 'AppBundle\Entity\Category',
|
||||
'csrf_protection' => false,
|
||||
'allow_extra_fields' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getBlockPrefix()
|
||||
{
|
||||
return '';
|
||||
}
|
||||
}
|
61
src/AppBundle/Resources/views/Category/index.html.twig
Normal file
61
src/AppBundle/Resources/views/Category/index.html.twig
Normal file
|
@ -0,0 +1,61 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title></title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
{{ form_row(form.name) }}
|
||||
|
||||
<pre id="response"></pre>
|
||||
|
||||
<input type="button" id="submit" value="Save">
|
||||
|
||||
<ul id="list">
|
||||
|
||||
</ul>
|
||||
|
||||
<script src="//codeorigin.jquery.com/jquery-1.10.2.min.js"></script>
|
||||
<script src="{{ asset('bundles/fosjsrouting/js/router.js') }}"></script>
|
||||
<script src="{{ asset('bundles/fosjsrouting/js/fos_js_routes.js') }}"></script>
|
||||
|
||||
<script>
|
||||
(function(w, $) {
|
||||
var listCategories = function() {
|
||||
$.ajax({
|
||||
url: Routing.generate('get_categories'), // Si tes scripts sont dans un fichiers JS
|
||||
}).done(function(collection) {
|
||||
var $list = $('#list').html('');
|
||||
|
||||
collection.forEach(function(value) {
|
||||
$list.append($('<li>').html(value.name));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
$('#submit').click(function() {
|
||||
var name = $('#name').val();
|
||||
|
||||
$.ajax({
|
||||
url: Routing.generate('post_category_new'),
|
||||
method: 'POST',
|
||||
data: {
|
||||
name: name,
|
||||
}
|
||||
}).done(function(response) {
|
||||
$('#response').text(JSON.stringify(response, true));
|
||||
listCategories();
|
||||
}).fail(function(response) {
|
||||
$('#response').text(JSON.stringify(response.responseJSON, null, 4));
|
||||
listCategories();
|
||||
});
|
||||
});
|
||||
|
||||
listCategories();
|
||||
})(window, jQuery);
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
|
Loading…
Reference in a new issue