Skip to content

Commit

Permalink
Added example codes
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Janda committed Jul 3, 2017
1 parent 58952ca commit 461a504
Show file tree
Hide file tree
Showing 17 changed files with 1,549 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Order Allow,Deny
Deny from all
24 changes: 24 additions & 0 deletions app/Controllers/AbstractController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace App\Controllers;

use App\Http\ApiResponseFormatter;
use Nette\Application\IPresenter;

abstract class AbstractController implements IPresenter
{

/**
* @var ApiResponseFormatter
*/
protected $apiResponseFormatter;


public function __construct(ApiResponseFormatter $apiResponseFormatter)
{
$this->apiResponseFormatter = $apiResponseFormatter;
}

}
26 changes: 26 additions & 0 deletions app/Controllers/ErrorController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace App\Controllers;

use App\Http\ApiResponse;
use Nette\Application\BadRequestException;
use Nette\Application\IResponse;
use Nette\Application\Request;

final class ErrorController extends AbstractController
{

public function run(Request $request): IResponse
{
$exception = $request->getParameter('exception');

/**
* @todo Log exception
*/

return new ApiResponse($this->apiResponseFormatter->formatException($exception));
}

}
32 changes: 32 additions & 0 deletions app/Controllers/LoginController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace App\Controllers;

use App\Http\ApiResponse;
use Nette\Application\IResponse;
use Nette\Application\Request;
use Ublaboo\ApiRouter\ApiRoute;

/**
* API for logging users in
*
* @ApiRoute(
* "/api/login",
* methods={
* "POST"="run"
* },
* presenter="Login",
* format="json"
* )
*/
final class LoginController extends AbstractController
{

public function run(Request $request): IResponse
{
return new ApiResponse($this->apiResponseFormatter->formatMessage('Hello'));
}

}
11 changes: 11 additions & 0 deletions app/Http/ApiResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace App\Http;

use Nette\Application\Responses\JsonResponse;

final class ApiResponse extends JsonResponse
{
}
41 changes: 41 additions & 0 deletions app/Http/ApiResponseFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace App\Http;

use Nette\Application\Responses\JsonResponse;

final class ApiResponseFormatter
{

public function formatMessage(string $message): array
{
return [
'status' => 'ok',
'payload' => [
'message' => $message
]
];
}


public function formatPayload(array $payload): array
{
return [
'status' => 'ok',
'payload' => $payload
];
}


public function formatException(\Exception $e): array
{
return [
'status' => 'error',
'code' => $e->getCode(),
'message' => $e->getMessage()
];
}

}
19 changes: 19 additions & 0 deletions app/Routing/RouterFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace App\Routing;

use Nette\Application\Routers\RouteList;

final class RouterFactory
{

public function create(): RouteList
{
$router = new RouteList;

return $router;
}

}
23 changes: 23 additions & 0 deletions app/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

require __DIR__ . '/../vendor/autoload.php';

$configurator = new Nette\Configurator;

//$configurator->setDebugMode(FALSE);
$configurator->enableTracy(__DIR__ . '/../log');

$configurator->setTimeZone('Europe/Prague');
$configurator->setTempDirectory(__DIR__ . '/../temp');

$configurator->addConfig(__DIR__ . '/config/config.neon');

if (file_exists(__DIR__ . '/config/config.local.neon')) {
$configurator->addConfig(__DIR__ . '/config/config.local.neon');
}

$container = $configurator->createContainer();

return $container;
26 changes: 26 additions & 0 deletions app/config/config.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
extensions:
apiRouter: Ublaboo\ApiRouter\DI\ApiRouterExtension


parameters:


application:
errorPresenter: Error
scanDirs: false
mapping:
*: App\Controllers\*Controller


session:
expiration: 14 days


services:
- App\Http\ApiResponseFormatter
- App\Routing\RouterFactory
router: @App\Routing\RouterFactory::create

# Api endpoints
#
- App\Controllers\LoginController
38 changes: 38 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "ublaboo/api-router-project",
"type": "project",
"description": "Example starter project for ublaboo/api-router",
"keywords": ["rest", "api", "routes", "nette", "router", "restapi", "restrouter", "routing", "route", "starter", "project", "example"],
"homepage": "https://github.com/ublaboo/api-router-project",
"license": ["MIT"],
"support": {
"issues": "https://github.com/ublaboo/api-router-project/issues"
},
"authors": [
{
"name": "Pavel Janda",
"homepage": "http://paveljanda.com"
}
],
"require": {
"php": ">= 7.1",
"nette/application": "^2.4",
"nette/bootstrap": "^2.4.2",
"nette/caching": "^2.5",
"nette/di": "^2.4",
"nette/http": "^2.4",
"nette/security": "^2.4",
"nette/utils": "^2.4",
"tracy/tracy": "^2.4",
"ublaboo/api-router": "^1.2"
},
"require-dev": {
"nette/tester": "^1.6"
},
"autoload": {
"psr-4": {
"App\\": "app"
}
},
"minimum-stability": "stable"
}
Loading

0 comments on commit 461a504

Please sign in to comment.