Skip to main content

Command Palette

Search for a command to run...

Laravel + Node.js: PHP in Watt Runtime

Updated
4 min read
Laravel + Node.js: PHP in Watt Runtime

We're pleased to announce that Laravel applications can now run within Node.js using the Platformatic PHP stackable. This integration brings Laravel's PHP framework into the Node.js ecosystem through Watt runtime, opening up new possibilities for full-stack JavaScript developers and PHP developers.

Why This Matters

The traditional separation between PHP and Node.js environments has long required developers to maintain separate infrastructures. With @platformatic/php, you can now:

  • Run Laravel applications within a Node.js process
  • Share resources and reduce infrastructure complexity
  • Leverage the Node.js ecosystem while maintaining your Laravel codebase
  • Deploy PHP applications alongside JavaScript microservices in a unified runtime

How It Works

The Platformatic PHP stackable uses @platformatic/php-node, a Rust-based native module that embeds a multi-threaded PHP runtime directly inside Node.js. This provides a high-performance bridge between the PHP and JavaScript worlds. Your Laravel application runs as a stackable module within Watt, where it has direct access to other services without needing to go to the network, substantially reducing latency.

Getting Started: Run Laravel in Node.js

Let's walk through setting up a Laravel application in Watt runtime. The process is quite straightforward.

Prerequisites

  • Node.js 20.6.0+
  • npm or your preferred package manager
  • A Laravel application (or create a fresh one)

Step 1: Set Up Your Project Structure

Create a new directory for your project and initialize it:

mkdir my-laravel-watt-app
cd my-laravel-watt-app
npm init -y

Step 2: Configure the Package Structure

Update your package.json to include workspaces and necessary dependencies:

{
  "name": "my-laravel-watt-app",
  "workspaces": [
    "web/*"
  ],
  "scripts": {
    "start": "wattpm start",
    "dev": "wattpm dev",
    "build": "wattpm build"
  },
  "dependencies": {
    "@platformatic/runtime": "^2.64.0",
    "platformatic": "^2.64.0",
    "wattpm": "^2.64.0"
  },
  "engines": {
    "node": ">=20.6.0"
  }
}

Step 3: Add Your Laravel Application

Create the workspace directory and add your Laravel application:

mkdir -p web/laravel
# Copy your Laravel application into web/laravel/
# Or create a fresh Laravel app:
cd web/laravel
composer create-project laravel/laravel .

Step 4: Configure the PHP Stackable

Create a package.json in your Laravel directory (web/laravel/package.json):

{
    "private": true,
    "type": "module",
    "scripts": {
        "start": "platformatic start"
    },
    "dependencies": {
        "@platformatic/php": "^0.5.2"
    }
}

Step 5: Create the Platformatic Configuration

The key configuration is in web/laravel/platformatic.json. This file tells Platformatic how to run your Laravel application:

{
  "$schema": "https://schemas.platformatic.dev/@platformatic/php/0.3.1.json",
  "module": "@platformatic/php",
  "php": {
    "docroot": "public",
    "rewriter": [
      {
        "conditions": [
          { "type": "not_exists" }
        ],
        "rewriters": [
          {
            "type": "href",
            "args": ["/(.*)", "/index.php?/$1"]
          }
        ]
      }
    ]
  },
  "watch": true
}

This configuration:

  • Sets the document root to Laravel's public directory
  • Configures URL rewriting to route all requests through index.php (essential for Laravel's routing)
  • Enables file watching for development

Step 6: Install Dependencies and Run

From your project root:

npm install
cd web/laravel
npm install
cd ../..
npm run dev

Your Laravel application is now running within the Node.js Watt runtime.

Configuration Deep Dive

The platformatic.json file is where the PHP stackable is configured. Let's break down the key components:

  • module: Specifies @platformatic/php as the stackable module
  • docroot: Points to Laravel's public directory where index.php resides
  • rewriter: Implements Laravel's front controller pattern, ensuring all requests are routed through index.php
  • watch: Enables hot reloading during development

What's Next?

This integration opens up several possibilities:

  • Unified Deployment: Deploy PHP and Node.js services together
  • Shared Resources: Optimize resource usage by running everything in a single runtime
  • Gradual Migration: Incrementally migrate PHP applications to JavaScript while maintaining existing functionality
  • Polyglot Microservices: Mix PHP, JavaScript, and other languages in your architecture

Advanced Features

The PHP stackable supports additional configuration options for more complex scenarios:

  • Custom PHP extensions
  • Environment variable management
  • Performance tuning options
  • Integration with other Platformatic services

Conclusion

The ability to run Laravel within Node.js through the Platformatic PHP stackable represents a significant step forward in runtime interoperability. Whether you're a PHP developer looking to leverage Node.js tooling or a JavaScript developer needing to integrate PHP applications, this solution provides a seamless bridge between both worlds.

We look forward to seeing what you'll build with this capability. The combination of Laravel and the Node.js ecosystem creates new opportunities for modern web development.

Ready to give it a try? Check out the complete example on GitHub and join our community to share your experiences!


The Platformatic PHP stackable is actively maintained and we welcome contributions. Visit our documentation for more details and advanced configuration options.