What are you using for PHP REST API?

mcmatak
Member | 504
+
+2
-

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
+
0
-

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.

mcmatak
Member | 504
+
0
-

what about authorization, any component you can add into you suggested projects?

echo
Member | 134
+
0
-

You can also try bulletphp.com

Honza Kuchař
Member | 1662
+
0
-

I used Nette for this. There was no performance issue, database was always way slower.

bazo
Member | 620
+
0
-

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

bazo
Member | 620
+
0
-

that's a nice library too

mcmatak
Member | 504
+
0
-

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
+
0
-

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
+
0
-

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.

addseo1115
Member | 1
+
0
-

Thanks for sharing the great information. Nice.

Jan Endel
Member | 1016
+
-11
-

btw are you sure with using php for this task? For REST api is IMHO best Node.js with express.

newPOPE
Member | 648
+
-1
-

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.

Tharos
Member | 1030
+
+5
-

@JanEndel But in your API you often want to use your domain logic you have already implemented in PHP (Nette) based application.

Jan Endel
Member | 1016
+
0
-

I only want to make a hint to discuss for this solution. :-)

Jan Tvrdík
Nette guru | 2595
+
0
-

Nette is not slow, but using Nette\Application\ UI \Presenter for APIs is just stupid =)

droa22
Member | 6
+
0
-

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
+
+1
-

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
+
0
-

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
+
+6
-

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.