denetorus / galaxy-application-rest-api
Runs PHP controller actions for HTTP requests and presents their results as JSON.
Package info
github.com/Denetorus/GalaxyApplicationRestApi
pkg:composer/denetorus/galaxy-application-rest-api
Requires
- php: >=8.4
- denetorus/galaxy-application: ^0.1.0
- denetorus/galaxy-logging: ^0.3.0
- denetorus/galaxy-result: ^0.3.0
- denetorus/galaxy-routing: ^0.2.0
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-12-03 16:02:30 UTC
README
Runs PHP controller actions for HTTP requests and presents their results as JSON.
Русский · Architecture · MIT
Getting started
Requires PHP 8.4 or newer. Install version 0.1.0 from Packagist.
composer require denetorus/galaxy-application-rest-api:0.1.0
From the source checkout, install dependencies and run
composer install
php -S 127.0.0.1:8080 examples/index.php
# In another terminal / В другом терминале
curl http://127.0.0.1:8080/hello
Complete example examples/index.php.
Put controllers in App\Controller\RestApi and configure that namespace in Composer. A GET request to /hello invokes HelloController::actionDefault(). A second path segment selects the action. Override getAllowedHttpMethods() to allow other verbs. A JSON body is available as $input['json']. Missing controllers and actions return 404, a method mismatch returns 405 with Allow, and malformed JSON returns 400. Each action returns GResult. JsonPresenter emits the response. The application can set status and headers explicitly.
Reading a JSON request
For a POST action, return ['POST'] from getAllowedHttpMethods() for that action. Send an object with the JSON content type.
curl -X POST -H 'Content-Type: application/json' -d '{"name":"Ada"}' http://127.0.0.1:8080/hello
The action reads the decoded payload from $input['json']. Use GApp::createHttpErrorResult(1000, 'Name is required.', 422) to return a validation error with a business code and an explicit HTTP status. The bundled GET example returns 405 for this POST until you enable it.