kematjaya / base-controller-bundle
base component for traditional web application
Installs: 2 169
Dependents: 1
Suggesters: 0
Security: 0
Stars: 3
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- doctrine/orm: ^2.10|^3.3
- doctrine/persistence: ^3.3
- giggsey/libphonenumber-for-php: ^8.12
- haydenpierce/class-finder: ^0.5
- kematjaya/hidden-type-bundle: ^7.0
- knplabs/knp-paginator-bundle: ^6.6
- spiriitlabs/form-filter-bundle: ^11.0
- symfony/config: ^6.0|^7.0
- symfony/dependency-injection: ^6.0|^7.0
- symfony/form: ^6.0|^7.0
- symfony/framework-bundle: ^6.0|^7.0
- symfony/http-foundation: ^6.0|^7.0
- symfony/http-kernel: ^6.0|^7.0
- symfony/property-access: ^7.0
- symfony/translation-contracts: ^3.5
Requires (Dev)
- phpunit/phpunit: ^9.0
- symfony/dom-crawler: ^6.0|^7.0
- symfony/var-dumper: ^6.0|^7.0
- dev-main
- 7.0.5
- 7.0.4
- 7.0.3
- 7.0.2
- 7.0.1
- 7.0
- 2.4.3
- 2.4.2
- 2.4.1
- 2.4
- 2.3.9
- 2.3.8
- 2.3.7
- 2.3.6
- 2.3.5
- 2.3.4
- 2.3.3
- 2.3.2
- 2.3.1
- 2.3
- 2.2.1
- 2.2
- 2.1.7
- 2.1.6
- 2.1.5
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.2
- 2.0.1
- 2.0
- 1.11.0
- 1.10.5
- 1.10.4
- 1.10.3
- 1.10.2
- 1.10.1
- 1.10.0
- 1.9.7
- 1.9.6
- 1.9.5
- 1.9.4
- 1.9.3
- 1.9.2
- 1.9.1
- 1.9.0
- 1.8.5
- 1.8.4
- 1.8.3
- 1.8.2
- 1.8.1
- 1.8.0
- 1.7.1
- 1.7
- 1.6
- 1.5
- 1.4
- 1.3
- 1.2
- 1.1
- 1.0
- dev-detached
This package is auto-updated.
Last update: 2024-11-20 14:18:02 UTC
README
Base component for symfony 5 application
- installation
composer require kematjaya/base-controller-bundle
- add to config/bundles.php
Kematjaya\BaseControllerBundle\BaseControllerBundle::class => ['all' => true]
- usage 3.1. Controller
use App\Entity\Foo;
use App\Form\FooType;
use App\Filter\FooFilterType;
use App\Repository\FooRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Kematjaya\BaseControllerBundle\Controller\BaseLexikFilterController as BaseController;
...
/**
* @Route("/foo", name="foo_")
*/
class FooController extends BaseController
{
/**
* @Route("/", name="index", methods={"GET", "POST"})
*/
public function index(Request $request, FooRepository $repo): Response
{
// create filter form objct
$form = $this->createFormFilter(FooFilterType::class);
// processing filter form
$queryBuilder = $this->buildFilter($request, $form, $repo->createQueryBuilder('this'));
return $this->render('foo/index.html.twig', [
'datas' => parent::createPaginator($queryBuilder, $request), // create pagination for data
'filter' => $form->createView()
]);
}
/**
* @Route("/new", name="new", methods={"GET","POST"})
*/
public function new(Request $request): Response
{
$foo = new Foo();
// processing form ajax
$form = $this->createForm(FooType::class, $foo, [
'attr' => ['id' => 'ajaxForm', 'action' => $this->generateUrl('foo_new')]
]);
$result = parent::processFormAjax($request, $form);
if ($result['process']) {
return $this->json($result);
}
// end processing ajax
// processing wihout ajax form
$form = $this->createForm(FooType::class, $foo, [
'action' => $this->generateUrl('foo_new')]
]);
$result = parent::processForm($request, $form);
if ($result['process']) {
return $this->redirectToRoute('foo_index');
}
// end processing wihout ajax form
return $this->render('foo/form.html.twig', [
'foo' => $foo,
'form' => $form->createView(), 'title' => 'new'
]);
}
/**
* @Route("/{id}/show", name="show", methods={"GET"})
*/
public function show(Foo $foo): Response
{
return $this->render('foo/show.html.twig', [
'foo' => $foo,
]);
}
/**
* @Route("/{id}/edit", name="edit", methods={"GET","POST"})
*/
public function edit(Request $request, Foo $foo): Response
{
// processing form ajax
$form = $this->createForm(FooType::class, $foo, [
'attr' => ['id' => 'ajaxForm', 'action' => $this->generateUrl('foo_edit', ['id' => $foo->getId()])]
]);
$result = parent::processFormAjax($request, $form);
if ($result['process']) {
return $this->json($result);
}
// end processing ajax
// processing wihout ajax form
$form = $this->createForm(FooType::class, $foo, [
'action' => $this->generateUrl('foo_edit', ['id' => $foo->getId()])
]);
$result = parent::processForm($request, $form);
if ($result['process']) {
return $this->redirectToRoute('foo_index');
}
// end processing wihout ajax form
return $this->render('foo/form.html.twig', [
'foo' => $foo,
'form' => $form->createView(), 'title' => 'edit'
]);
}
/**
* @Route("/{id}/delete", name="delete", methods={"DELETE"})
*/
public function delete(Request $request, Foo $foo): Response
{
$tokenName = 'delete'.$foo->getId();
parent::doDelete($request, $foo, $tokenName);
return $this->redirectToRoute('foo_index');
}
}
3.2. Form Type
// src/Form/FooType.php
...
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Kematjaya\BaseControllerBundle\Type\PhoneNumberType;
use Kematjaya\BaseControllerBundle\Type\DateRangeType;
...
class FooType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
// Phone Number Type
$builder->add('phone', PhoneNumberType::class, [
'label' => 'phone'
]);
// Date Range Type
$builder->add('phone', DateRangeType::class, [
'label' => 'phone',
'from_options' => ['widget' => 'single_text'],
'to_options' => ['widget' => 'single_text']
]);
}
}
- add to config/packages/twig.yaml
twig:
form_themes:
- '@BaseController/phone_number_layout.html.twig'
3.3 Filter
- filter form base on LexikFormFilterBundle : https://github.com/lexik/LexikFormFilterBundle
- usage:
// src/Filter/FooFilterType.php
...
use Symfony\Component\Form\FormBuilderInterface;
use Kematjaya\BaseControllerBundle\Filter\AbstractFilterType;
...
class FooFilterType extends AbstractFilterType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('roles', Filters\ChoiceFilterType::class, [
'choices' => [],
// query json use JSONQuery(),
// date range filter use dateRangeQuery(),
// float / integer range use floatRangeQuery()
'apply_filter' => $this->JSONQuery(),
//
]);
}
}