1. Go to this page and download the library: Download alex-kalanis/restful library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
namespace ResourcesModule;
use kalanis\Restful\IResource;
use kalanis\Restful\Application\UI\ResourcePresenter;
/**
* SamplePresenter resource
* @package ResourcesModule
*/
class SamplePresenter extends ResourcePresenter
{
protected $typeMap = array(
'json' => IResource::JSON,
'xml' => IResource::XML
);
/**
* @GET sample[.<type xml|json>]
*/
public function actionContent(string $type = 'json'): void
{
$this->resource->title = 'REST API';
$this->resource->subtitle = '';
$this->sendResource($this->typeMap[$type]);
}
/**
* @GET sample/detail
*/
public function actionDetail(): void
{
$this->resource->message = 'Hello world';
}
}
use kalanis\Restful\Application\Routes\ResourceRoute;
$anyRouteList[] = new ResourceRoute('sample[.<type xml|json>]', 'Resources:Sample:content', ResourceRoute::GET);
/**
* SamplePresenter resource
* @package Restful\Api
*/
class SamplePresenter extends BasePresenter
{
public function validateCreate(): void
{
$this->input->field('password')
->addRule(IValidator::MIN_LENGTH, NULL, 3)
->addRule(IValidator::PATTERN, 'Please add at least one number to password', '/.*[0-9].*/');
}
public function actionCreate(): void
{
// some save data insertion
}
}
namespace Restful\Api;
use kalanis\Restful\Application\UI\ResourcePresenter;
/**
* Base API ErrorPresenter
* @package Restful\Api
*/
class ErrorPresenter extends ResourcePresenter
{
/**
* Provide error to client
* @param \Exception $exception
*/
public function actionDefault($exception): void
{
$this->sendErrorResource($exception);
}
}
use kalanis\Restful\Application\UI\SecuredResourcePresenter
/**
* My secured resource presenter
*/
class ArticlesPresenter extends SecuredResourcePresenter
{
// all my resources are protected and reachable only for logged user's
// you can also add some Authorizator to check user rights
}
namespace ResourcesModule;
use kalanis\Restful\Security\Process\SecuredAuthentication;
/**
* CRUD resource presenter
* @package ResourcesModule
*/
class CrudPresenter extends BasePresenter
{
#[\Nette\DI\Attributes\Inject]
public SecuredAuthentication $securedAuthentication;
protected function startup(): void
{
parent::startup();
$this->authentication->setAuthProcess($this->securedAuthentication);
}
// your secured resource action
}
namespace Restful\Api;
use kalanis\Restful\IResource;
use kalanis\Restful\Security\Process\AuthenticationProcess;
use kalanis\Restful\Security\Process\OAuth2Authentication;
/**
* CRUD resource presenter
* @package Restful\Api
*/
class CrudPresenter extends BasePresenter
{
#[\Nette\DI\Attributes\Inject]
public AuthenticationProcess $authenticationProcess;
/**
* Check presenter
yaml
php:
zlib.output_compression: yes
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.