# Contributte / ApiRouter

# Content

# Installation

ApiRouter is available on composer:

composer require contributte/api-router
1

# Usage

# Configure

At first register compiler extension.

extensions:
	apiRouter: Contributte\ApiRouter\DI\ApiRouterExtension
1
2

Don't forget to register your controller/presenter/endpoint classes.

services:
	- App\Controllers\LoginController
	- App\Controllers\PingController
1
2
3

# Using annotation

Example of used annotations. Don't forget to import Contributte\ApiRouter\ApiRoute, it's required by doctrine/annotations (opens new window).

namespace App\ResourcesModule\Presenters;

use Nette\Application\UI\Presenter;
use Contributte\ApiRouter\ApiRoute;

/**
 * API for managing users
 *
 * @ApiRoute(
 * 	"/api-router/api/users[/<id>]",
 * 	parameters={
 * 		"id"={
 * 			"requirement": "\d+",
 * 			"default": 10
 * 		}
 * 	},
 *  priority=1,
 *  presenter="Resources:Users"
 * )
 */
class UsersController extends Presenter
{

	/**
	 * Get user detail
	 *
	 * @ApiRoute(
	 * 	"/api-router/api/users/<id>[/<foo>-<bar>]",
	 * 	parameters={
	 * 		"id"={
	 * 			"requirement": "\d+"
	 * 		}
	 * 	},
	 * 	method="GET"
	 * )
	 */
	public function actionRead($id, $foo = NULL, $bar = NULL)
	{
		$this->sendJson(['id' => $id, 'foo' => $foo, 'bar' => $bar]);
	}


	public function actionUpdate($id)
	{
		$this->sendJson(['id' => $id]);
	}


	public function actionDelete($id)
	{
		$this->sendJson(['id' => $id]);
	}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

Now 3 routes will be created (well, 2, but the one accepts both PUT and DELETE method).

If you don't want to create route with DELETE method, simply remove the UsersPresenter::actionDelete() method.

# Using Nette Router

namespace App;

use Contributte\ApiRouter\ApiRoute;use Nette;use Nette\Application\Routers\Route;use Nette\Application\Routers\RouteList;

class RouterFactory
{

	/**
	 * @return Nette\Application\IRouter
	 */
	public function createRouter()
	{
		$router = new RouteList;

		/**
		 * Simple route with matching (only if methods below exist):
		 * 	GET     => UsersPresenter::actionRead()
		 * 	POST    => UsersPresenter::actionCreate()
		 * 	PUT     => UsersPresenter::actionUpdate()
		 * 	DELETE  => UsersPresenter::actionDelete()
		 */
		$router[] = new ApiRoute('/hello', 'Users');

		/**
		 * Custom matching:
		 * 	GET  => UsersPresenter::actionSuperRead()
		 * 	POST => UsersPresenter::actionCreate()
		 */
		$router[] = new ApiRoute('/hello', 'ApiRouter', [
			'methods' => ['GET' => 'superRead', 'POST'],
		]);

		$router[] = new ApiRoute('/api-router/api/users[/<id>]', 'Resources:Users', [
			'parameters' => [
				'id' => ['requirement' => '\d+', 'default' => 10],
			],
			'priority' => 1,
		]);

		$router[] = new ApiRoute('/api-router/api/users/<id>[/<foo>-<bar>]', 'Resources:Users', [
			'parameters' => [
				'id' => ['requirement' => '\d+'],
			],
			'priority' => 1,
		]);

		# Disable basePath detection
		$route = new ApiRoute('/api-router/api/users', 'Resources:Users');
		$route->setAutoBasePath(false);
		$router[] = $route;

		$router[] = new Route('<presenter>/<action>', 'Homepage:default');

		return $router;
	}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

# Api documentation

There is another extension for Nette which works pretty well with ApiRouter: ApiDocu (opens new window). ApiDocu generates awesome api documentation from your RESTful routes. It can also show you documentation in application runtime!

# Examples

We've made a few skeleton with preconfigured contributte/api-router.

https://github.com/contributte/api-router-skeleton (opens new window)