PHP CS Fixer – How to keep your code clean

Code sur écran d'ordinateur

A really useful tool to keep your code clean is PHP Coding Standards Fixer. It allows us to follow the PHP standards defined in PSR-1, PSR-2, etc. It is also possible to configure it to follow the style of your team with a documentation. The recommended way to install it is outside your code, but … Read more

PHPUnit – How to test a controller that throws an error

Code sur écran d'ordinateur

Let’s say we have a method in a controller like this: /** * @Route(path=”/add”, name=”add”, methods={“POST”}) */ public function add(Request $request,): Response { throw new BadRequestHttpException(‘Error’); } To test that the exception is properly thrown, just write this public function testList(): void { $this->expectException(BadRequestHttpException::class); $client = self::createClient(); $client->catchExceptions (false); $crawler = $client->request( Request::METHOD_POST, ‘/add’ ); … Read more

Symfony4 how to create a custom error page 404

Code sur écran d'ordinateur

You have to start by installing the necessary bundle: composer require symfony/twig-pack When done, create the template error404.html.twig templates/ └─ bundles/ └─ TwigBundle/ └─ Exception/ └─ error404.html.twig Example of the error404.html.twig template {% extends ‘base.html.twig’ %} {% block body %} <h1>Page not found</h1> <p> The requested page couldn’t be located. Checkout for any URL misspelling … Read more

WordPress – how to install WP-CLI on Ubuntu

MacBook avec code sur un bureau

Start by downloading wp-cli curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar Then give it the right to be executed chmod +x wp-cli.phar Then move it to /usr/local/bin to be able to use the wp command sudo mv wp-cli.phar /usr/local/bin/wp To use the command, simply go into the folder containing the WordPress installation $ wp NAME wp DESCRIPTION Manage WordPress … Read more

Search and replace in the terminal

MacBook Pro affichant un éditeur de code

On Ubuntu it is possible to search and replace a word or phrase with a simple command line sed -i ‘s/original/nouveau/g’ fichier.txt Explanation: sed = Stream EDitor -i = in-place (save in the original file) the strings in the command: s = the replacement command original = regular expression for the word to be replaced. … Read more

Upload multiple files to an entity with Symfony4 and VichUploaderBundle

Code sur écran d'ordinateur

Preparation   To begin with, we need a Product entity as well as a ProductImage entity that will contain the names of the images. To do this, we use the command in our terminal VichUploaderBundle Setup As stated in the Symfony 4 documentation, we will use the VichUploaderBundle Then add the necessary “config/packages/vich_uploader.yaml” configuration file … Read more

Symfony – debug a Twig template with dump()

Code sur écran d'ordinateur

Twig’s {{ dump() }} function is very handy to inspect a variable inside a template. But without the right package installed, the output is unreadable. Here is how to get a clean, collapsible display like this one: Install the DebugBundle The nice output comes from Symfony’s VarDumper component, enabled by the DebugBundle. Install it as … Read more

Xdebug installation with PHP 5.6 on Ubuntu

MacBook avec code sur un bureau

First of all you have to install the xdebug package for PHP 5.6 sudo apt-get install php-xdebug Then you have to enable it so it is accessible from PhpStorm for example Edit the file sudo nano /etc/php/5.6/mods-available/xdebug.ini and add the following lines xdebug.remote_enable = on xdebug.remote_connect_back = on xdebug.idekey = “vagrant” The PHP service needs … Read more

Categories PHP