Expose Backend

Your stack:

Goal

Turn a PHP class into a remotely callable backend service using Graftcode Gateway - no Laravel routes, no REST controllers, no OpenAPI specs needed.

What You'll See

  • Create a small PHP class with public methods.
  • Host it through Graftcode Gateway using Docker.
  • Explore the exposed methods in Graftcode Vision - your service is now accessible from any app as a strongly-typed client.

Prerequisites

  • Docker installed and running
  • PHP 7.4 or 8.0+ and Composer only if you run the Composer consumer locally - Docker alone is enough to build and host the service

Step 1. Create a project folder

Create a new folder and set up a Composer project:

mkdir php-energy-service
cd php-energy-service

Create composer.json:

{
    "name": "example/energy-service",
    "description": "Energy price sample module",
    "type": "library",
    "license": "ISC",
    "version": "1.0.0",
    "require": {
        "php": "^7.4 || ^8.0"
    },
    "autoload": {
        "psr-4": {
            "Energy\\": "src/"
        }
    }
}

Step 2. Write a PHP class with public methods

Create src/EnergyPriceCalculator.php:

<?php

declare(strict_types=1);

namespace Energy;

use Random\RandomException;

class EnergyPriceCalculator
{
    /**
     * @throws RandomException
     */
    public static function getPrice(): int
    {
        return random_int(100, 104);
    }
}

This is a plain PHP class - no attributes, no frameworks, no special interfaces. Any public method you write here will automatically become available for remote consumption once hosted through Graftcode Gateway.

Step 3. Host it with Graftcode Gateway

Create a Dockerfile in the project root:

FROM php:8.4-cli

WORKDIR /usr/app

COPY . /usr/app/

RUN apt-get update && apt-get install -y git unzip \
 && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer \
 && composer install --no-dev --optimize-autoloader \
 && curl -fsSL grft.dev/get/gg | sh && dpkg -i gg.deb && rm gg.deb \
 && apt-get clean && rm -rf /var/lib/apt/lists/*

EXPOSE 80

CMD ["gg", "/usr/app/src"]

The key line is the last one - gg (Graftcode Gateway) reads the module path under --modules, discovers all public methods in your PHP classes, and exposes them automatically. The --port listener (default 80) carries WebSocket traffic (for example ws://localhost:80/ws) and serves HTTP for Graftcode Vision, MCP, and /status on the same TCP port (for example http://localhost:80/GV). Gateway also starts a second HTTP listener on --httpPort (default 81); you can map that in Docker too, but one host port is enough for this tutorial.

Build and run the container:

docker build --no-cache --pull -t myenergyservice-php:test .
docker run -d -p 80:80 --name graftcode_demo_php myenergyservice-php:test

Your PHP service is now running and exposed through Graftcode Gateway.

Step 4. Explore the service in Graftcode Vision

Open http://localhost:80/GV in your browser.

You will see all public methods from your PHP class - their names, parameter types, and return types. Graftcode Vision also provides:

  • A "Try it out" button to call methods live, directly from the browser.
  • A package manager command (npm, NuGet, PyPI, Composer, etc.) to install this service as a strongly-typed client in any other application.

Everything above works without any account - perfect for learning and local development. When you're ready for real-world usage, create a free account at portal.graftcode.com, set up a project, and copy its Project Key.

Then pass the key when starting your gateway:

CMD ["gg", "/usr/app/src", "--projectKey", "YOUR_PROJECT_KEY"]

A Project Key gives you:

  • Stable registry URL - consumers always find and update your Graft through a permanent address, so install commands don't change when you redeploy.
  • Portal visibility - see all your gateways and exposed services in one place at gateways.graftcode.com.
  • Access control - decide who can download your Grafts using package manager authentication and permissions.
  • MCP endpoint - Graftcode Gateway automatically exposes an MCP (Model Context Protocol) endpoint alongside your service, making your methods callable by AI agents and LLM-based tools out of the box.

Step 6. Call it from another app

Your service is now accessible from any application. From Graftcode Vision, select your target package type - for example Composer - and copy the generated install command. That installs a Graft: a strongly-typed client that lets any app call your service methods directly.

Note

Test the Graft locally with a Composer consumer

This step shows a call snippet only. To create a separate consumer project (composer.json, Graft repositories, composer install, Hypertube post-install scripts, and vendor/autoload.php), follow Connect Microservices — PHP, Step 3: Install the Graft. Use the Composer install command, registry URL, and package name from Vision at http://localhost:80/GV after Step 4 above—not the hosted demo URLs in that tutorial. Set GraftConfig::$host to your gateway WebSocket endpoint (for this tutorial, typically ws://localhost:80/ws). Prefer use namespaces from the installed Graft package or Vision, not only from the example below.

Use the package name and import paths from Vision or Gateway logs (for this sample the package is typically graft/composer-energy-service:1.0.0). getPrice() is synchronous and returns an int:

<?php

declare(strict_types=1);

require __DIR__ . '/vendor/autoload.php';

use Graft\Composer\EnergyService\EnergyPriceCalculator;
use Graft\Composer\EnergyService\GraftConfig;

GraftConfig::$host = 'ws://localhost:80/ws';

$price = EnergyPriceCalculator::getPrice();
echo sprintf('Energy price is: %d $', $price), PHP_EOL;

Always prefer the exact namespace, package id, and install command from Graftcode Vision - generated Composer Grafts mirror your hosted class names (often camelCase method names such as getPrice).

No REST clients, no request/response models, no endpoint URLs in your code. When you add or update a public method, consumers update their Graft with a single package manager command.