ApiRouter

Downloading DataGrid

ApiRouter is available through composer package ublaboo/api-router:

composer require ublaboo/api-router

Routing

ApiRouter lets you easily set your RESTful api routes. You can do that directly (in RouterFactory):

$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']
]);

return $router;

or via Annotation (learn more):

/**
 * @ApiRoute(
 * 	"/users"
 * )
 */
class ApiRouterPresenter extends Nette\Application\UI\Presenter
{

	public function actionCreate() {
		$this->sendJson(['John' => 'Doe']);
	};


	public function actionRead() {
		$this->sendJson(['John' => 'Doe']);
	};

}

This route will match HTTP methods READ and POST to appropriate actions. If you want to associate methods to different actions, you can do it by creating @ApiRoute annotation above particular method:

/**
 * @ApiRoute(
 * 	"/users"
 * )
 */
class ApiRouterPresenter extends Nette\Application\UI\Presenter
{

	public function actionCreate() {
		$this->sendJson(['John' => 'Doe']);
	};


	/**
	 * @ApiRoute(
	 * 	"/api-router/api/users/<id>[/<limit>-<offset>]",
	 *	method="GET",
	 * )
	 */
	public function actionSuperRead() {
		$this->sendJson(['John' => 'Doe']);
	};

}

Configuration

If you want to route application via annotations, start with registering extension in your config.neon:

extensions:
	apiRouter: Ublaboo\ApiRouter\DI\ApiRouterExtension
Tento web používá k poskytování služeb a analýze návštěvnosti soubory cookie. Používáním tohoto webu s tím souhlasíte. V pořádku Další informace