A simple API

Let's leave the form processing behind and continue with something completely different.

Introducing the api extension, we will now build a simple JSON API for our previously created product list.

The ApiController

To implement this, we will use the ApiController class.

Create a new file ApiProductsController.php in the directory Application/Controllers/ with the following content:

<?php declare(strict_types=1); namespace Application\Controllers; use BlueMvc\Api\ApiController; class ApiProductsController extends ApiController { public function getAction() { return ['Foo' => 'Bar']; } }

Open Application/Setup/ApplicationSetup.php and add a new route for the controller:

<?php declare(strict_types=1); namespace Application\Setup; use Application\Controllers\ApiProductsController; use Application\Controllers\ErrorController; use Application\Controllers\IndexController; use Application\Controllers\ProductsController; use BlueMvc\Core\Interfaces\ApplicationInterface; use BlueMvc\Core\Route; use BlueMvc\Twig\TwigViewRenderer; use DataTypes\FilePath; /** * The application setup. */ class ApplicationSetup { /** * Sets up the application. * * @param ApplicationInterface $application The application. */ public static function setup(ApplicationInterface $application): void { // Set up view handling. $application->addViewRenderer(new TwigViewRenderer()); $application->setViewPath(FilePath::parse('Application/Views/')); // Set up routes. $application->addRoute(new Route('', IndexController::class)); $application->addRoute(new Route('products', ProductsController::class)); $application->addRoute(new Route('api/products', ApiProductsController::class)); $application->setErrorControllerClass(ErrorController::class); } }

Note that it is possible to set a multi-level path for a route, e.g. api/products. This is also true for ordinary controllers.

Open the url http://localhost/api/products/ and check the result:


So, what just happened?

For an ApiController, the action method name corresponds to the http method used.

In this case, the browser sent a GET request, which invoked the getAction method (action method names are case insensitive for ApiControllers).

Also, the return value from the action method is automatically JSON-encoded for everything that is not an ActionResult or null.

Let's try it with our product list instead.

The product list

Open Application/Controllers/ApiProductsController.php and modify the content to look like this:

<?php declare(strict_types=1); namespace Application\Controllers; use Application\Models\Product; use BlueMvc\Api\ApiController; class ApiProductsController extends ApiController { public function getAction() { return Product::getAll(); } }

Reload the http://localhost/api/products/ page to see the new result:

[{"Id":"foo","Name":"24 inch LCD Monitor","Price":140},{"Id":"bar","Name":"USS Enterprise Scale Replica","Price":20}]

As expected, this is the product list in JSON-format.

That concludes this short introduction to the api extension.

In the next part of this tutorial, the website will be tested using PHPUnit.

✎ Published in category get started