What are you using for PHP REST API?
- mcmatak
- Member | 499
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?
Comments
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
5 years agothat'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.
5 years ago- Tharos
- Member | 1042
@JanEndel But in your API you often want to use your domain logic you have already implemented in PHP (Nette) based application.
Comments
I only want to make a hint to discuss for this solution. :-)
5 years ago- Caine
- Member | 218
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;
- echo
- Member | 134
- newPOPE
- Member | 659
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.
- 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
- olivedev
- Member | 2
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/ ).
- bazo
- Member | 625
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.
Comments
what about authorization, any component you can add into you suggested projects?
5 years agoI used Nette for this. There was no performance issue, database was always way slower.
5 years agoi 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
5 years agoi 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
5 years agoNette is not slow, but using Nette\Application\ UI \Presenter for APIs is just stupid =)
5 years ago- Jan Endel
- Member | 1026
btw are you sure with using php for this task? For REST api is IMHO best Node.js with express.