Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Tomas Votruba:
How To Convert All Your Symfony Service Configs to Autodiscovery
Jan 03, 2019 @ 17:51:26

Tomas Votruba has a tutorial posted to his site showing you how to update your Symfony application to make all of your services use auto-discovery rather than hard-coded configuration settings.

Do you use Symfony autodiscovery services registration everywhere and your configs have no extra lines? Skip this post and rather read another one.

But if you have many configs with manual service registration, tagging, and autowiring, keep reading. I'll show you how you can convert them easily be new Symplify package.

He starts off by talking about a few e-commerce projects he's been working with lately that define service configurations manually. He then mentions a package that's been created to help convert these over easily to autodiscovery rather than having to change them one by one. He provides the instructions to use this package and mentions some of the things that could go wrong in the conversion process to keep an eye out for.

tagged: tutorial convert symfony configuration autodiscovery package cli

Link: https://www.tomasvotruba.cz/blog/2018/12/27/how-to-convert-all-your-symfony-service-configs-to-autodiscovery/

Tomas Votruba:
5 Gotchas of the Bin File in PHP CLI Applications
Aug 02, 2018 @ 17:47:03

Tomas Votruba has a new post to his site sharing five "gotchas" in CLI applications as it relates to the "bin" file.

This post from Master PHP CLI Apps with Symfony cluster will focus on bin files. It's the smallest part of PHP CLI Application, so I usually start with it.

Yet, there are still a few blind paths you can struggle with. I'll drop a few extra tricks to make your bin file clean and easy to maintain.

He starts with a brief definition of what a "bin" file is before getting into his list of "gotchas":

  • recommendations about naming and location of the file
  • setting it up to be autoloaded by Composer
  • including the right "shebang" to have it executed by the correct program
  • changing access rights
  • symlinking in Composer

The post ends with the complete code required to build a simple Symfony CLI application that will autoload libraries correctly and be executable by the system's php binary.

tagged: gotcha top5 list cli commandline application symfony tutorial

Link: https://www.tomasvotruba.cz/blog/2018/08/02/5-gotchas-of-the-bin-file-in-php-cli-applications/

Laravel News:
Beautiful PHP CLI Menus for Laravel Artisan Commands
Feb 05, 2018 @ 16:33:53

On the Laravel News site today there's a post that highlights a package that makes it easier to create beautiful CLI menus for artisan commands in the Laravel framework.

Nuno Maduro, author of Laravel Zero and Collision (which nunomaduro/laravel-console-menu.

The post includes an example of code that can create the menu and its options. It also shows how you can change the look of the default menu, changing colors, the "selected" marker, padding and width. The package makes use of the php-school/cli-menu package for a lot of the base menu rendering and functionality.

tagged: tutorial menu cli artisan command laravel package

Link: https://laravel-news.com/beautiful-php-cli-menus-laravel-artisan-commands

Robert Basic:
CLI command to whitelist Composer packages
Dec 04, 2017 @ 15:35:37

Robert Basic has shared a quick tip for the Composer users out there (you do use Composer, right?) showing how to exclude certain packages from updates without having to whitelist packages all the time.

Given that Composer has no --exclude flag or similar, the only other option is to create a list of packages we allow to be updated, excluding the ones we don’t want to be updated. We need to create a whitelist.

Creating it manually would be a PITA though, especially if there’s a lot of packages to include or exclude. CLI to the rescue!

He includes a command that grabs the packages from the current composer info listing (using grep, sed, cut and paste). He walks through the command showing how it works to pull the package information out. With the help of the -v option for grep it's easy to remove certain items from the list (blacklist) and to provide a string back to composer that can then be used to update only the remaining packages.

tagged: composer package commandline cli whitelist blacklist

Link: https://robertbasic.com/blog/cli-command-to-whitelist-composer-packages/

Rob Allen:
Getting started with Serverless PHP
Aug 24, 2017 @ 14:19:19

In a new post to his site Rob Allen talks about getting up and running with "serverless PHP" using the Apache OpenWhisk cloud platform that executes as events are fired rather than sitting as an entire application.

I've been interested in Apache OpenWhisk for a little while now and recently submitted a new feature to add PHP support to the project. As OpenWhisk is a serverless environment, most users do not run their own copy and instead use a commercial provider with IBMs Bluemix available now along with Adobes I/O Runtime and RedHat coming soon. As a result, my contribution, isn't practically useful until it's in production with a provider.

Fortunately, and remarkably quickly, IBM have added support for PHP to the Bluemix "IBM Cloud Functions" platform, so now we can use PHP to develop serverless applications and deploy them into the wild! This is a rebranding, so you'll see this referred to as "Bluemix OpenWhisk" around the web too.

He walks you through the setup of an account on the bluemix.net service and how to install OpenWhisk locally rather than the IBM "Bluemix CLI" tool. He then shares the code to create a simple "Hello World" function in PHP (7.1) and push it up to your account. He then executes the action from the command line, returning his JSON formatted greeting. Next he shows how to set this function up as a web action and give it a URL for the outside world to access. He finishes the post with some examples of calling the URL and how it would receive parameters via a POST or a GET.

tagged: serverless development ibm bluemix function web cli tutorial

Link: https://akrabat.com/getting-started-with-serverless-php/

SitePoint PHP Blog:
Re-Introducing Symfony Console – CLI PHP for the Uninitiated!
May 25, 2017 @ 16:38:02

The SitePoint PHP blog has posted a tutorial from author Claudio Ribeiro that wants to re-introduce you to the Symfony Console package, a component of the larger Symfony framework that makes it easier to create and work with command-line PHP scripts.

As software developers, we often feel the need to resort to command line tools. These kinds of tools are helpful when we need to do a sort of recurring task like migrating data, performing imports, or creating cron jobs.

The Symfony Console component tool provides us with a simple framework to create our own command line tools. Unlike many components in Symfony, this is a standalone package and is used by the likes of Laravel‘s Artisan and many other famous PHP packages.

The tutorial then walks you through the installation process, via Composer, and the creation of a new command. With this simple base created, he then adds in actual functionality, building out a command to hash and verify a password string. They show how to use the command and an example of its output. Next up, he creates another command example, this time verifying the password hash provided as an argument. The tutorial wraps up with a look at testing your console comamnds with PHPUnit tests via the included CommandTester functionality.

tagged: symfony console commandline cli package component tutorial introduction

Link: https://www.sitepoint.com/re-introducing-symfony-console-cli-php-uninitiated/

Loïc Faugeron:
The Ultimate Developer Guide to Symfony - CLI Example
Apr 07, 2016 @ 15:43:51

Loïc Faugeron has posted another in his "ultimate guide" series of posts around components in the Symfony framework. In this latest post he gives an example of using the command line component with the Console component.

In this guide we've explored the main standalone libraries (also known as "Components") provided by Symfony to help us build applications: HTTP Kernel and HTTP Foundation, Event Dispatcher, Routing and YAML, Dependency Injection and Console. We've also seen how HttpKernel enabled reusable code with Bundles, and the different ways to organize our application tree directory.

Finally we've started to put all this knowledge in practice by creating a "fortune" project with: an endpoint that allows us to submit new fortunes, a page that lists all fortunes. In this article, we're going to continue the "fortune" project by creating a command that prints the last fortune.

He walks through the use of an example repository as a base and shows:

  • the creation of the command class
  • the matching tests to ensure it's working correctly
  • building out the logic to pull in the latest fortunes

They enter the fortunes via the web interface and use the command line to output them as as simple text.

tagged: ultimate developer guide symfony commandline cli example tutorial series

Link: https://gnugat.github.io/2016/04/06/ultimate-symfony-cli-example.html

Geert Eltink:
Zend-Expressive Console CLI Commands
Feb 12, 2016 @ 17:21:15

In a new post to his site Geert Eltink shares how he added console command support to Zend Expressive, a PSR-7 framework from Zend that recently hit it's v1.0 mark.

zend-expressive does not come out of the box with a console for handling cli commands. However it's easy to add this and make full use of the container and its dependencies.

He uses the Symfony console component to handle most of the "heavy lifting" with the command line interaction, pulled in via Composer. He shows the process for getting the component installed and how to create the "bootstrap" file needed to build an instance of the Application class. He follows this with a simple "greeting" command including the configuration to add a few arguments and output the simple "Hello" message. He then creates the functionality to wire it in to the Zend Expressive application and gives an example of it in use.

tagged: zend expressive framework console command commandline cli tutorial symfony component

Link: https://xtreamwayz.com/blog/2016-02-07-zend-expressive-console-cli-commands

SitePoint PHP Blog:
Console Wars – PHP CLI Libraries
Jul 27, 2015 @ 14:32:35

The SitePoint PHP blog has a post that compares some of the major PHP CLI libraries, three of them at least: the Symfony console component, Hoa console and the Webmozart solution.

I have always been a big fan of console commands and I try to provide a command line interface (CLI) as much as possible in most of my PHP projects. In this article, I’ll briefly compare three PHP console command libraries.

He starts with a brief history on each of the libraries, talking about their origins and age. He then talks about the necessary dependencies each requires and the overall complexity of the code they include. Next up is some practical examples putting each to use outputting a simple message back to the user using user input for both the message and output color.

tagged: console commandline library symfony hoa webmozart cli compare

Link: http://www.sitepoint.com/console-wars-php-cli-libraries/

Michelangelo van Dam:
popen for cli commands and pipes in php
May 05, 2015 @ 15:53:38

Michelangelo van Dam has a quick new post to his site talking about popen and pipes in command-line PHP as an alternative to the "exec" functions PHP provides to make command lines calls.

I got a question today about using commands that pipe output to other commands within PHP applications. There are two functions in PHP that are perfect for the task: popen and proc_open. But when you want to run it as a complete process, you can go about using exec, shell_exec, passthru or system and fiddle with escapeshellcmd. But often this looks messy and not reusable. A better approach would be to use "popen".

He includes a code example of how to use this method, showing a call to a command line tool and piping the results back into a PHP variable for later use. You can find out more about the use of popen in the PHP manual and accompanying examples.

tagged: popen procopen commandline cli pipe result example

Link: http://www.dragonbe.com/2015/05/popen-for-cli-commands-and-pipes-in-php.html


Trending Topics: