Discuss your project

Category

PHP

41 articles Page 3

Symfony 4 / Sonata: Create a REST API

In this article, we will see how to create a REST API with FOS/RestBundle, including authentication, and a Swagger-like documentation generator. List of necessary bundles:friendsofsymfony/rest-bundle: Provides a set of tools to help develop a RESTful APIhttps://github.com/FriendsOfSymfony/FOSRestBundlejms/serializer-bundle: Allows object serialization.https://packagist.org/packages/jms/serializer-bundlelexik/jwt-authentication-bundle: Manages JSON web tokens.htt...

Symfony 4 / Sonata: Install the Sonata Page CMS

Sonata is a suite of bundles for Symfony 4. One of which allows for content management in a WordPress-like manner.There is little documentation available on the internet, and for good reason, the installation is bugged, and the few people who have dealt with the bug tracker on GitHub, have been bluntly told to RTFM.Staying put with such failure is not really my style.Here's how to install and configure Sonata-Page-Bundle, with the official installation, and the workarounds for these F###ing ...

Symfony 4 / Sonata: Create a CSV export page

The default interfaces of Sonata are CRUDs. This is incredibly practical (otherwise, we wouldn't use it). But an administration is not only composed of CRUDs. Here we will see how to create a simple data export page, by removing the default views of the interfaces, and creating our own to manage our export button. 1 – Adding the export library composer require sonata-project/exporter We then need to add a configuration file for our exporter. #config/packag...

Symfony 4 / Sonata: Create a nested interface

We are going to see how to build an admin interface composed of several tables that have Many2Many relationships.Let's revisit our example of a many/many interface available hereWe have a zone table, which is made up of several elements of the departments table. On these departments, we have agencies.To top it off, and to give meaning to this data chain, we add a zx_credential table, which represents salespeople.Here is our data chain: Salespeople->Zones->Departments->Agencies. ...

Symfony 4 / Sonata: create a ManyToMany administration

We will develop here all the manipulations to build a many to many interface via Sonata.We will take as an example a zone management with a connection by departments. Each zone is thus constituted of multiple connections with the department table.In MysqlWorkbench, it looks like this: To generate the entities, we use the following command: php bin/console doctrine:mapping:import "App\Entity" annotation --path=src/Entity Next, we ...

Symfony / Sonata: Add a clone function in a CRUD

To add an object duplication feature to a CRUD list, you need to modify the list by adding the button, configure a route, execute object duplication code, and finally, reference the controller that will host our function in our interface, via its service.Adding the button:In your controller, add the button by referencing the button template.Here we set the path 'Admin/list__action_clone.html.twig'. The system will look for the file at: /templates/Admin/list_...

Symfony 4 / Sonata: Create a nested CRUD interface (child 1->N) with drag-and-drop ordering

In short, we want to create a CRUD interface, 1N, with which, when we are editing an item, we add a panel to manage all the child items.Here we have a wtype table, with a wconf table that contains a series of records linked to a wtype item. Just like for the implementation example of sortable with drag'n'drop ( available here ) we are going to use the following components:pixassociates/sortable-behavior-bundle and stof/doctrine-extension...

Symfony 4 / Sonata: Manage order with drag'n drop

You need to install pixassociates/sortable-behavior-bundle and stof/doctrine-extensions-bundle composer require stof/doctrine-extensions-bundle composer require pixassociates/sortable-behavior-bundle Add the configuration in pix_sortable.yamlBy adding the entity and the field in position_field (here wconf, and the position field) pix_sortable_behavior: db_driver: orm # mongodb default value : orm position_field: default: position #default value : position entities: ...

Symfony 4 / Sonata: using form types

Here is a list of common form types used in admin interfaces.Date Time PickerAdd the template in the config/packages/twig.yaml file twig: form_themes: - '@SonataCore/Form/datepicker.html.twig' And in the controller: $formMapper->with('Date d\'activation', ['class' => 'col-md-4 abcdaire']) ->add('activation_debut', DatePickerType::class, ['required' => false, 'label'=>'Activation début','attr' => ['placeholder' => '']]) ...

Symfony 4 / Sonata add a filter related to a OneToMany entity

Filters are managed in the configureDatagridFilters method protected function configureDatagridFilters(DatagridMapper $datagridMapper) { $filers=$datagridMapper->getAdmin()->getFilterParameters(); } One can add a simple filter on a title or any parameter of our entity.The show_filter parameter allows to display it when the page loads. Combined with the value of $filters, one can specify to keep the element displayed after the filter submission.For example, one can set the filter to ...

Symfony 4 / Sonata: Use a primary key with a /

As strange as it may seem, I had to use an entity with a primary key set on a varchar field, and some values contained a " / ".Until you are faced with the problem, it's impossible to imagine that it will cause an issue. And then, the drama unfolds. An exception has been thrown during the rendering of a template ("Parameter "id" for route "admin_app_wtype_edit" must match "[^/]++" ("MACHIN/CHOSE" given) to generate a corresponding URL."). In short, ...

Symfony 4 / Sonata: Create a OneToMany (1N) administration

We are going to create a 1N administration interface, with two entities. The first one, One, and the second, Many, and set up an admin panel for the One table, which can affect several elements of the Many table. To spice things up, we'll add some additional parameters, such as timestamp fields for sync dates with an SI, and primary fields that are not called ID and are not auto-incremented. In our case, we have a sync with an SI that requires a timestamp fi...