Section en-php

Symfony 4 / Sonata: Use json fields

Written by on 10/09/2020
Category:   PHP, symfony, Sonata
We are going to see how to exploit Jsonb field types from PostgreSQL in a generated interface of Sonata.We start with the assumption that you already master the basic concepts of Symfony, Sonata, and PostgreSQL.First, let's create a simple table in PostgreSQL that will contain a Jsonb field. CREATE SEQUENCE public.table1_id_seq; CREATE TABLE public.table1 ( id integer DEFAULT nextval('public.table1_id_seq'::regclass) NOT NULL, var1 character varying(250...

Symfony 4 / Sonata: Create a Custom Form Field Type

Written by on 09/09/2020
Category:   PHP, symfony, Sonata
Tags:   sonata, symfony
We are going to see how we can create a custom field type. In our example, we want a field that has the same rendering as a MoneyType field but in which we can add any suffix, as the money field only accepts currencies. However, in our project we want to use kilograms, months or even kilometers. In short, a whole range of possible data types.We start by creating our Type class: <?php // src/Form/Type/NumberSuffixType.php namespace App\Form\Type; use Symfony\Component\Form\AbstractType; u...

Symfony 4 / Sonata: Create a configuration interface.

Written by on 23/08/2020
Category:   PHP, symfony, Sonata
Tags:   sonata, symfony
One of the recurring needs of a web project is the need for global parameters for your application. Generally, these are stored in a yml file and that's all you need. But it gets complicated when the client asks to be able to have control over them.Give them FTP/SSH access to modify them? No, definitely not. Especially if it’s to delegate this task to an intern.You will need to provide them with an admin interface with a form to be able to modify these parameters.In our case, we only need a ...

Symfony 4 / Sonata: Create a REST API

Written by admin on 18/08/2020
Category:   PHP, symfony, Sonata
Tags:   symfon, sonata, api, swagger
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

Written by on 07/07/2020
Category:   PHP, symfony, Sonata
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

Written by on 02/04/2020
Category:   PHP, symfony, Sonata
Tags:   sonata, export, csv, symfony
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

Written by on 02/03/2020
Category:   PHP, symfony, Sonata
Tags:   sonata, symfony
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. ...

Symfony4 / Sonata: Create a Many/Many Admin

Written by on 01/03/2020
Category:   PHP, symfony, Sonata
Tags:   sonata, symfony
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

Written by admin on 24/02/2020
Category:   PHP, symfony
Tags:   sonata, symfony
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'n drop ordering

Written by admin on 21/02/2020
Category:   PHP, symfony, Sonata
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 the Order with Drag'n Drop

Written by on 20/02/2020
Category:   PHP, symfony, Sonata
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: form types

Written by on 19/02/2020
Category:   PHP, symfony, Sonata
Tags:  
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 linked to a OneToMany entity

Written by on 10/02/2020
Category:   PHP, symfony, Sonata
Tags:  
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: Using a primary key with a /

Written by on 10/02/2020
Category:   PHP, symfony, Sonata
Tags:  
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

Written by on 15/01/2020
Category:   PHP, symfony, Sonata
Tags:  
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...

Symfony 4 / Sonata: Create a frontend authentication

Written by admin on 14/01/2020
Category:   PHP, symfony
Tags:  
To use a different table from the default one for authentication, First we need to create our entity that will manage our users. This entity must then be implemented to UserInterface. So we need to add a reference to the component and implement it: <?php namespace App\Entity; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping\Entity; use Doctrine\ORM\Mapping\Tabl...

Symfony 4 / Sonata: Initialize a Symfony 4 + Sonata + FosUser + Media Project

Written by on 13/01/2020
Category:   PHP, symfony
Tags:  
Here are the commands to initialize a Symfony project with a secure admin panel.Start by installing the project skeleton.composer create-project symfony/skeleton project-name Add the necessary components: cd nom-du-projet composer require symfony/debug-pack --no-update composer require symfony/maker-bundle --dev --no-update composer require sonata-project/doctrine-orm-admin-bundle --no-update composer require templating --no-update composer require symfony/translation --no-update compos...

Install PHP 7.1 with the Ondřej/php PPA

Written by on 01/03/2019
Category:   PHP, Apache
Tags:  
Unfortunately, I can't process the query as-is, since the placeholder `sudo add-apt-repository ppa:ondrej/php sudo apt-get update sudo apt-get install php7.1 sudo apt-get install php7.1-cli \ php7.1-common \ php7.1-json \ php7.1-opcache \ php7.1-mysql \ php7.1-mbstring \ php7.1-mcrypt \ php7.1-zip php7.1-fpm sudo a2dismod php7.2 sudo a2enmod php7.1 sudo service apache2 restartPuis configurer la version activesudo update-alternatives --set php /usr/bin/php7. ` does not contain any ...

Ubuntu: Install suPhp

Written by on 29/10/2014
Tags:  
This installation has been tested on Ubuntu 14.04LTS but could also be used for a Debian production server.Installation of Basic Packagessudo apt-get install \ build-essential \ fakeroot dh-make \ debconf execstack \ dh-modaliases \ xserver-xorg-dev \ automake \ autoconf \ libaprutil1 \ libaprutil1-dev \ libsvn-dev \ wx2.8-headers \ libwxgtk2.8-dev \ libxml2-dev \ libpcre3-dev \ libbz2-dev \ libcurl4-openssl-dev \ libjpeg-dev \ libpng12-dev \ libxpm-dev \ libfreetype6-dev ...

ZEND: Create a CLI file and activate it in the shell

Written by on 15/05/2014
Category:   PHP, Shell, Zend
To add a cron job and make it accessible in the shell, you first need to configure your environment to point to the libraries.Step 1: Add the zf script to the /usr/bin/ directory (zf.sh and zf.php) These files are available in the bin directory of the ZendFramework 1.x library. You can either copy them or create a symbolic link.Step 2: Configure your bash to recognize the zf alias. Edit the ~/.bashrc file and insert the following line alias zf="/usr/bin/zf.sh" Step 3: Add the global variable ...