Back

Laravel Zero with PHP Binaries

Laravel Zero with PHP Binaries

As a Laravel developer, you're already familiar with the elegance and power of the Laravel ecosystem. But did you know that Laravel Zero allows you to harness that same power to build command-line tools? In this guide, we'll walk through creating a CLI tool using Laravel Zero, then compile it into a standalone PHP binary using Static PHP CLI (SPC). This workflow is perfect for building utilities, automation scripts, or deployment tools.

Laravel Zero with PHP Binaries

In this article, we will explore how to use Laravel Zero with PHP binaries. We will cover the following topics:

  1. Setting Up Laravel Zero: Create a new CLI project and customize it for your needs
  2. Building Custom Commands: Add powerful functionality to your command-line tool
  3. Compiling into a Standalone Binary: Use Static PHP CLI to create a portable, dependency-free executable
  4. Distributing Your Tool: Package and share your CLI application with users across different environments

Step 1: Setting Up Laravel Zero

To start, create a new Laravel Zero project:

composer create-project --prefer-dist laravel-zero/laravel-zero inspire-cli
cd inspire-cli

This command installs Laravel Zero and sets up the project structure. The --prefer-dist flag ensures only the necessary files are downloaded, speeding up the installation.

By default, Laravel Zero names the application "application". To rename it to something more meaningful, run:

php application app:rename inspire

After renaming, the CLI tool will be accessible via the inspire command:

php inspire

You can test the default "inspiring" command that comes with Laravel Zero:

php inspire inspiring

Step 2: Building Custom Commands

Laravel Zero makes it easy to create custom commands using the familiar Laravel command structure. You can generate a new command using:

php inspire make:command FetchQuote

This creates a new command class in the app/Commands directory. You can then implement your command logic in the handle() method.

Step 3: Compiling into a Standalone Binary

Laravel Zero includes a built-in compiler that can create a PHAR archive of your application. However, to create a truly standalone binary that doesn't require PHP to be installed on the target system, we'll use Static PHP CLI (SPC).

First, build your Laravel Zero application:

php inspire app:build

This creates a PHAR file in the builds directory.

Next, download and set up Static PHP CLI:

# Download SPC for your platform (macOS/ARM64 example)
gh release --repo crazywhalecc/static-php-cli download 2.5.1 -p spc-macos-aarch64.tar.gz
tar -xzf spc-macos-aarch64.tar.gz && chmod a+x spc

Prepare the PHP extensions needed for your application:

EXTENSIONS="bcmath,calendar,ctype,curl,dba,dom,exif,filter,fileinfo,iconv,mbstring,mbregex,openssl,pcntl,pdo,pdo_mysql,pdo_sqlite,phar,posix,readline,simplexml,sockets,sqlite3,tokenizer,xml,xmlreader,xmlwriter,zip,zlib,sodium"

# Download PHP source and extension dependencies
./spc download --with-php=8.2 --for-extensions="$EXTENSIONS"

# Build PHP with the required extensions
./spc build --build-micro --build-cli "$EXTENSIONS"

Finally, combine your Laravel Zero application with the static PHP build to create a standalone binary:

mkdir -p bin && ./spc micro:combine builds/inspire -O bin/inspire

You now have a standalone binary in the bin directory that can be run on any compatible system without PHP installed.

Step 4: Distributing Your Tool

Your standalone binary can now be distributed to users across different environments. Since it doesn't require PHP or any dependencies to be installed, it's perfect for distribution via package managers, direct downloads, or internal deployment systems.

Consider these distribution options:

  • GitHub Releases for public tools
  • Internal package repositories for company tools
  • Direct distribution for client-specific tools

Remember to include documentation on how to use your CLI tool, including available commands and options.

Conclusion

Laravel Zero combined with Static PHP CLI provides a powerful way to build and distribute command-line tools. You get all the benefits of Laravel's elegant syntax and powerful features, packaged in a standalone binary that can run anywhere. This approach is perfect for creating utilities that need to be distributed to users who may not have PHP installed or configured correctly.

By following this guide, you've learned how to set up Laravel Zero, build custom commands, compile your application into a standalone binary, and distribute it to users. Now you can create powerful CLI tools that leverage your Laravel expertise while being easy to distribute and use.

Follow me on