getinstance / api_util
core getinstance api code
Requires
- aura/cli: ~2.1.0
- dflydev/fig-cookies: ^3.0
- getinstance/commons: 1.*
- guzzlehttp/guzzle: ^7.5
- odan/session: ^6.1
- php-di/php-di: ^7.0
- phpmailer/phpmailer: ^6.9
- slim/psr7: ^1.6
- slim/slim: ^4.11
- slim/twig-view: ^3.3
- symfony/http-kernel: 4.*
- symfony/psr-http-message-bridge: ^2.1
Requires (Dev)
- dev-master
- v7.0.3
- v7.0.2
- v7.0.1
- v7.0.0
- v6.0.3
- v6.0.2
- v6.0.1
- v6.0.0
- v5.0.0
- v4.2.0
- v4.1.5
- v4.1.4
- v4.1.3
- v4.1.2
- v4.1.1
- v4.1.0
- v4.0.2
- v4.0.1
- v3.0.1
- v3.0.0
- v2.0.5
- v2.0.4
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- v1.8.0
- v1.7.0
- v1.6
- v1.5
- v1.0.4
- v1.0.3
- v1.0.1
- v1.0.0
- dev-develop
- dev-minor-tweaks-for-static-analysis-compliance
- dev-update-index-skel-twig2
- dev-slim-upgrade
- dev-fix-v2_0_4
- dev-feature/codegen
- dev-feature/composer-migrate
This package is auto-updated.
Last update: 2025-10-10 09:17:05 UTC
README
Web and REST tools built largely on top of Slim (previous iterations were based more substantially initially on Silex and subsequently Symfony components)
Initial installation
composer require getinstance/api_util
Generating a skeleton
You don't need to do this, but it can save time.
Skeleton: server mode
TODO - create a core-only generation.
api_util includes some skeleton generation tools. This one generates core components and an API endoint
$ php ./vendor/getinstance/api_util/bin/tools.php genendpoint dashserver getops\\dashboard\\server hello world,universe
genendpointis the subcommanddashserveris the name of the app. It should consist only of alphanumeric characters without symbols, punctuation, or spaces.myorg\\dashserveris the package name (the double backslashes are required: The package will be rendered asmyorg\dashserverin namespace and use statements)hellois the endpoint name -- so we'll be callingGET /hello,POST /helloetcworld,universeare the fields relating the endpoint. There should be nothing but a comma between the fields.
This command will generate a controller, commands, a model class, a mapper. It will generate SQL for Mariadb. This will need to be tweaked since it does not add indexes and uses only the VARCHAR data type.
You can run the tool multiple times for different endpoints and the system will refuse to clobber existing files, writing them instead to the local tmp/ directory. You can then cut and paste the generated content.
TODO - improve tool to support a descriptor of multiple endpoints. Allow for injection of new endopoint into existing system (probably using comment tokens in source).
In the above case genendpoint wrote:
/scripts/*-- schemas and small scripts- In particualar here look at
create-db.sqlandcreate-schema.sql. The first creates the database instance and corresponding database user. The second is the schema definitions of the database generated by the first script.
- In particualar here look at
web/index.php-- the entry point to the system.src/controller/Controller.php-- theInitWareInterfaceimplementing component that runs routing. In the mainindex.php, an instance of this is added to theapi_utilFrontControllercomponent that handles the application lifecycle.api_utiluses Slim's routing components
src/command/*.phpclasses for implementing GET, PUT, POST, DELETE endpoints for manipulating Hello data.src/model/Hello.php-- a model class encapsulating the endpoint data.src/model/HelloMapper.php-- writes and retrieves Hello data from the databasedockerbuild/anddocker-compose.yaml-- files for running the project in a Docker container
Skeleton: web mode
api_util was originally written as a quick way of generating APIs for micro-services. However, it has proved useful also as a Web micro-framework. As such it has evolved to support quite a few Web-UI related tools.
Here is how to generate a Web UI skeleton:
php ./vendor/getinstance/api_util/bin/tools.php genweb dashfe getops\\dashboard\\fe
This command generates the same core elements as the genendpoint command. Instead of individual command classes, though it creates a single controller command file: src/command/Main.php. It also generates a Twig template.
Here is the rundown of generated files:
/scripts/*-- schemas and small scriptsweb/index.php-- the entry point to the system.src/controller/Controller.php-- theInitWareInterfaceimplementing component that runs routing. In the mainindex.php, an instance of this is added to theapi_utilFrontControllercomponent that handles the application lifecycle.api_utiluses Slim's routing components
src/command/Main.phpa central controller class which can be extended with new controller methods as needed.dockerbuild/anddocker-compose.yaml-- files for running the project in a Docker container
Running the skeleton in Docker
Whether you have generated a API or a Web project you should be able to run a test instance in the same way if you have Docker installed:
docker compose up
This will generate an instance running on PHP's in-built server and accessible via localhost port 8080. If, as we often do, you wish to create both an API and a Web instance that work together, we recommend you combine their docker-compose.yaml files into a single instance that sits above both your repositories. You will want to adjust the public ports in this case so that they don't collide.
TODO - strip the Mariadb container from the Web generation templates
InitWare: Routing and Configuration
The main controller will invoke any classes which implement getinstance\api_util\controller\InitWareInterface. Here is that interface:
namespace getinstance\api_util\controller;
use DI\ContainerBuilder;
use Slim\App;
interface InitWareInterface
{
public function handleConfiguration(ContainerBuilder $builder, Conf $conf): void;
public function handleRouting(App $app): void;
}
You will likely only need to create one of these implementing classes for your routing. You can add this to the system in your index.php by calling FrontController::addInitWare()
require_once __DIR__.'/../vendor/autoload.php';
use getinstance\api_util\controller\FrontController;
use sulis\admonkey\controller\Controller;
$controller = new FrontController();
$controller->addInitWare(new Controller());
$controller->init(['confpath'=>__DIR__."/../conf/", 'confname'=>'myapp']);
$response = $controller->execute();