Discuss your project

Category

PHP

41 articles Page 2

PHP FFI: callbacks — part 3

The CallbackDuring the preparation of this article, I noticed that many resources on the internet directly use the libc for their examples. You can easily find fairly demonstrative and simple examples. But to do something cool and original, I thought to myself "let's find something to show with this libc too". After all… if it's just passing a structure as most examples do, it won't be useful. And then I came across a function in a forum where people were ...

PHP FFI: Passing Parameters — Part 2

Calling the PHP library directly?What if we had fun writing a piece of code that is useless? Come on!!! Come on!! Come on!!!Alright OK. I propose we do something useless by using PHP to call a C library that uses the Zend Engine.Actually, this paragraph is not as useless as it seems. Its purpose is to show you a particular behavior 😊PHP-FFI has a few limitations: you can't pass a PHP variable directly to retrieve its dynamic value from the external library.You only have access to passing C la...

PHP FFI: Introduction — Part 1

IntroductionSince version 7.4 of PHP, we finally have access to interoperability functions between different languages and PHP: the FFI or Foreign Function Interface.What is FFI? It is simply the ability to use an external library (.dll or .so) directly in PHP, without having to create a PHP extension. Yes, with just a PHP script! It is, among other things, what has made Python so famous and allowed it to have so many features.The great thing about FFI is that a PHP developer who is not an e...

Symfony 6 / Sonata 5: Installation of SonataClassification.

We have seen in previous articles how to initialize a Symfony 6 + Sonata 5 project with media and users in a secure admin. We then looked at how to manage user localizations.Now we will see how to install and manage classifications. We proceed with the installation of the latest version to date. php8.1 composer.phar require sonata-project/classification-bundle:5.x-dev Apparently, we have a configuration error. Unrecognized option "media" under "sonata_classification.class". Availabl...

Symfony 6 / Sonata 5: Link locales and the time zone to user preferences.

We saw in a previous article how to install Symfony 6 + Sonata Admin 5 with user and media management. We will continue this installation with the configuration of languages and time zones. We will install intlBundle which allows to manage localized display. php8.1 composer.phar require sonata-project/intl-bundle We take this opportunity to add a timezone and locale field to the user.To do this, we will extend userBundle and add a field in the admin...

Symfony 6 / Sonata 5: Initialize a Symfony 6 project + Sonata admin 5 + UserBundle + MediaBundle

Here are the commands to initialize a Symfony project with a secure admin. To run Symfony 6 we will need to install php8.0 or php8.1 For php8.0 sudo apt-get install php8.0-cli libapache2-mod-php8.0 php8.0-common php8.0-opcache php8.0-igbinary php8.0-imagick php8.0-msgpack php8.0-readline php8.0-memcached php8.0-xml php8.0-mbstring php8.0-gd php8.0-mysql php8.0-curl php8.0-intl php8.0-memcache php8.0-memcached memcached libapache2-mod-php8.0 php8.0-zip php8.0-mysql For...

Symfony 4 / Sonata: Create a second interface with the same entity

Here we will see how to create a second interface while the entity is already in use in a CRUD interface.The problem with automatically generated interfaces is that they use the name of the entity to construct the routes.If we configure a second interface with an entity that is already used, we will end up with the same route as the first.To address this issue, we will use these two parameters in our admin class to override the default values.Suppose we are using the entity "model", we could...

Symfony 4 / Sonata: managing a multilingual administration interface

We will see how to set up a multilingual admin interface with a language selection button (language switcher).Installing translationBundle composer require sonata-project/translation-bundle bin/console assets:install The language switcher requires a specific twig filter.Otherwise, you will get an error like:Unknown "language_name" filter.Then, you need to install these two bundles composer require twig/intl-extra composer require twig/extra-bundle bin/console cache:clear We add the default c...

Symfony 4 / Sonata: managing a multi-server administration interface

We will see how to manage an admin interface that connects to multiple servers.In our example, we have set up a default MySQL server that manages our admin interface. Users, media, etc.And we have set up a PostgreSQL server that contains a table we wish to manage.We can add as many servers as we wish. Our MySQL Server We have traditionally set up a Symfony Sonata instance by default with a classic MySQL connection. So we can create our database, the schema, and...

Symfony 4 / Sonata: Using JSON fields

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

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 settings interface.

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 ...