What are you using for PHP REST API?
- mcmatak
- Member | 504
I just considering https://apigility.org/ or https://github.com/…cast/Restler, what do you think?
Do you need nette for API?
What about the situation if you have whole project in nette and you need the url /api/ to served the rest api server?
I would appreciate dependency injection in my api project and also configuration of nette. Some tools like webalize and tracy. Nothing else, or you think that there is need to use anything else from nette?
How you combine the api project and nette frontend project. I think both of them need to share the same models, like Doctrine database models. Anything else?
- bazo
- Member | 620
no you don't. rest on nette is really slow. all you need is nette di and some rest router you like.
i've been using this http://zaphpa.org/ for a few projects, then i made this https://github.com/…/rest-router for myself.
- Honza Kuchař
- Member | 1662
I used Nette for this. There was no performance issue, database was always way slower.
- bazo
- Member | 620
i use request signing via public and private key. you can do that yourself in few lines. or if you need something else i always wanted to try out the symfony firewall.
nette presenters add a significant overhead to rest calls. there's a lot of reflection and unnecessary services instantiation going on.
and if you want the api to be really really fast use phalcon fw :) but it's very low level
- mcmatak
- Member | 504
i went through all this suggestions, and finally i decided to use the restler https://github.com/…cast/Restler, trying to play with it.
it looks very lightweight, it has some authentication schemes to plug in, many examples, documentation creation, swagger ui implementation and lot of tests
seems like a good solution
- mcmatak
- Member | 504
on restler is problem DI, how you on other REST api libraries use DI? also i tried to implement nette/di to restler, but i am not sure if it is also performance killer, bcs, there is also some reflections and i cant use something like this
$sql = BadUsing::context()->getByType('\DibiConnection');
also BadUsing::context i dont like, bcs i dont know how to inject any object to api class constructor
maybe the best way is to left nette/di and use just static folder for services? not so much nice code
- bazo
- Member | 620
that's exactly why i didn't use restler. it's already an app with given structure.
this how my index.php looks like using my lib
<?php
$configurator = require __DIR__ . '/../../app/bootstrap.php';
$container = $configurator->createContainer();
$app = new Bazo\Rest\App;
$acclimator = new \Acclimate\Container\ContainerAcclimator;
$acclimated = $acclimator->acclimate($container);
$resolver = new \Bazo\Rest\Callbacks\DefaultCallbackResolver($acclimated);
$router->setCallbackResolver($resolver);
$app->attach($router);
$app->run();
?>
and router factory
<?php
public function createRESTRouter()
{
$router = new Router;
$router[] = new \Bazo\Rest\Route(['/transactions/{id}', [
'id' => Patterns::PATTERN_ALPHA,
]], [
Methods::POST => ['@transactions', 'post'],
Methods::GET => [Transactions::class, 'get'],
Methods::PATCH => [Transactions::class, 'patch'],
Methods::DELETE => [Transactions::class, 'delete'],
]);
return $router;
}
?>
Transactions is just a regular class registered as service in config.neon with methods post, get, patch, delete.
but the lib is not complete and last update was 3 months ago.
- newPOPE
- Member | 648
The fact is that Nette is not very suitable for creating APIs. Why? Because Nette does not have tools for creating REST, doesn't have any model layer and so on.
In my opinion Nette is right tool for creating UI.
Maybe sometimes somebody create micro framework based on Nette components like Lumen from Laravel, Slim, Silex and Zend has Apigility I think.
@mcmatak Be first and build micro FW for APIs based on Nette components.
- Jan Tvrdík
- Nette guru | 2595
Nette is not slow, but using Nette\Application\ UI \Presenter for APIs is just stupid =)
- droa22
- Member | 6
Although it is an old topic, maybe can be interest to someone:
Top 12 Best PHP RESTful Micro Frameworks (Pro/Con)
http://www.gajotres.net/…-frameworks/
In my opinion the best option for REST + PHP is PHP Phalcon (https://phalconphp.com).
REST Tutorial from official docs:
https://docs.phalconphp.com/…al-rest.html
REST examples from PHP Phalcon users:
https://phalconist.com/category/rest
And example of performance vs Laravel. Running a simple query that retrives all of the posts and displays them in order of their ‘id’ in a view (https://forum.phalconphp.com/…alcon-v1-3-1):
Laravel v4.1
- Functions Executed : 961 different functions
- Time to execute : 2050ms
Phalcon v1.3.1
- Functions Executed : 57 different functions
- Time to execute : 70ms
- Caine
- Member | 216
What about something like this?
class MicroRoute extends Route {
/** @var array */
private $callbacks;
/**
* @param string URL mask, e.g. '<presenter>/<action>/<id \d{1,3}>'
* @param int flags
*/
public function __construct($mask, $flags = 0) {
parent::__construct($mask, function ($presenter) {
/* @var MicroPresenter $presenter */
$request = $presenter->getRequest();
$method = $request->getMethod();
if (isset($this->callbacks[$method])) {
foreach ($this->callbacks[$method] as $callback) {
$callback($request, $presenter);
}
} else {
$presenter->error('NOT_IMPLEMENTED');
}
}, $flags);
}
public function get($callback) {
$this->callbacks['GET'][] = $callback;
return $this;
}
public function post($callback) {
$this->callbacks['POST'][] = $callback;
return $this;
}
public function delete($callback) {
$this->callbacks['DELETE'][] = $callback;
return $this;
}
public function patch($callback) {
$this->callbacks['PATCH'][] = $callback;
return $this;
}
}
and then usage:
$rest = new MicroRoute('rest/<domain>/<action>');
$rest->get(function (Request $request, $presenter) {
$parameters = $request->getParameters();
// CODE
});
$rest->post(function (Request $request, $presenter) {
});
$router[] = $rest;
- olivedev
- Member | 3
Lumen, Silex and Slim are a preferred choice for me and my colleagues when it comes to creating REST API. I am using Lumen since it goes well with Laravel. It is one of the top PHP micro frameworks right now (https://www.cloudways.com/…loud-server/ ).
- Felix
- Nette Core | 1196
Hi there!
There is a newer alternative library you can use, it's called Apitte.
It's heavily based on annotations, controllers instead of presenters and supports PSR-7.
/**
* @Path("/hello")
*/
final class HelloController implements IController
{
/**
* @Path("/world")
* @Method("GET")
*/
public function index(ApiRequest $request, ApiResponse $response): ApiResponse
{
return $response->writeBody('Hello world!');
}
}
You can find many examples on Github, https://github.com/…e/playground.
There is also prepared full-function example project based on Apitte + Nettrine (Doctrine) + Contributte libraries called Apitte Skeleton.
If you gonna need a help or something, don't hesitate to reach us at Forum / Gitter / Slack / email.