Name: Restler
Owner: AOE
Description: Simple and effective multi-format Web API Server to host your PHP API as Pragmatic REST and / or RESTful API
Forked from: Luracast/Restler
Created: 2016-02-13 19:07:49.0
Updated: 2016-12-29 20:12:52.0
Pushed: 2017-10-13 10:45:42.0
Homepage: http://luracast.com/products/restler/
Size: 5575
Language: JavaScript
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
![Gitter](https://badges.gitter.im/Join Chat.svg)
Restler is a simple and effective multi-format Web API Server written in PHP.
Just deal with your business logic in php, restler will take care of the REST!
if you do not have PHP >= 5.3.2 on your server and wont be able to upgrade you may use Restler 2 instead
@url GET my/custom/url/{param}
PHPDoc comments[planned]
Header to Method parameter mapping[planned]
ETag, If-None-Match support[planned]
Last-Modified, If-Modified-Since support[planned]
Unit Testing using PHPUnitMost stable and recent version is maintained at the master
branch, previous
versions are kept in the branches such as v1
and v2
Version branch with the current version such as v3
is used for building up
the next release. It's documentation may not be updated frequently and thus
reserved for the daring ones.
Feature branches such as features/html
and features/router
are purely
for experimentation purpose to try out a feature
Make sure PHP 5.3.2 or above (at least 5.3.4 recommended to avoid potential bugs) is available on your server
Restler uses Composer to manage its dependencies.
First, download a copy of composer.phar
. It can be kept in your project folder
or ideally in usr/local/bin
to use it globally for all your projects. If you
are on Windows, you can use the composer
windows installer instead.
You may install Restler by running the create project command in your terminal. Replace {projectName} with your actual project name. It will create a folder with that name and install Restler.
composer.phar create-project luracast/restler {projectName}
Note:-
If you do not want the additional formats and BDD tools you can include
--no-dev
to enforce exclusion of dev packages.If you want to try the bleading edge v3 branch or any of the feature branches include
3.x-dev
ordev-features/html
in the above command
Once Composer is installed, download the latest version of the Restler
framework and extract its contents into a directory on your server. Next, in the
root of your Restler project, run the php composer.phar install
(or composer install
) command to install all of the framework's dependencies.
This process requires Git to be installed on the server to successfully complete
the installation.
If you want to update the Restler framework, you may issue the
php composer.phar update
command.
Note:- If are not allowed to install composer and git on your server, you can install and run them on your development machine. The resulting files and folders can be uploaded and used on the server.
Ideally public folder should be mapped as your web root, It is optional, but recommended to avoid exposing unneeded files and folders.
Try the live examples in your localhost
Update the base_url specified in behat.yml
and then try the following command
behat
This will test the examples against the behaviors expected, for example
ure: Testing CRUD Example
Scenario: Creating new Author with JSON
Given that I want to make a new "Author"
And his "name" is "Chris"
And his "email" is "chris@world.com"
And the request is sent as JSON
When I request "/examples/_007_crud/authors"
Then the response status code should be 200
And the response should be JSON
And the response has a "id" property
All set, Happy Restling! :)
Once you have got restler installed with the above steps, you can quickly create your application by following these steps
Create your API classes with all needed public and protected methods
Create the gateway (index.php) as follows
p
ire_once '../../../vendor/restler.php';
Luracast\Restler\Restler;
new Restler();
addAPIClass('YourApiClassNameHere'); // repeat for more
handle(); //serve the response
Enable URL Rewriting
Make sure all the requests are routed to index.php by enabling URL Rewriting for your website
For example:-
If you are on Apache, you can use an .htaccess file such as
ctoryIndex index.php
odule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ index.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
Module>
odule mod_php5.c>
php_flag display_errors On
Module>
Note:- This requires
AllowOverride
to be set toAll
instead ofNone
in thehttpd.conf
file, and might require some tweaking on some server configurations. Refer to mod_rewrite documentation for more info.
If you are on Nginx, you have to make sure you set the server_name
and pass the
PHP scripts to fast cgi (PHP-FPM) listening on 127.0.0.1:9000
server {
listen 80;
server_name api.luracast.com; //change it to match your server name
//... other stuff
location ~ \.php$ {
root /var/www/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
include fastcgi_params;
}
//... other stuff
}
Note:- This requires PHP, PHP-FPM to be properly installed and configured. Refer to PHP FastCGI example for more info.
Fine tune to suit your needs
p
ire_once '../../../vendor/restler.php';
Luracast\Restler\Restler;
Luracast\Restler\Defaults;
t the defaults to match your requirements
ults::$throttle = 20; //time in milliseconds for bandwidth throttling
tup restler
new Restler();
addAPIClass('YourApiClassNameHere'); // repeat for more
addAPIClass('Resources'); //from restler framework for API Explorer
addFilterClass('RateLimit'); //Add Filters as needed
handle(); //serve the response
If you have successfully completed Installation Step 2, you should have
Restler API Explorer installed
in vendor/Luracast/explorer
folder. Create a symbolic link of
vendor/Luracast/explorer/dist
or copy the folder and name it as explorer
Place the explorer in the same folder as the index.php
Explore the api and try it out by openings explorer/index.html
from the web
root on your browser
Happy Exploring! :)
Note:- Using eAccelerator can make restler to fail as it removes the comments. More info can be found here
Restler supports annotations in the form of PHPDoc comments for API fine tuning
They are documented in detail under Annotations
In order to protect your api, authenticate and allow valid users
p
ire_once '../../../vendor/restler.php';
Luracast\Restler\Restler;
new Restler();
addAPIClass('YourApiClassNameHere'); // repeat for more
addAuthenticationClass('CustomAuth'); //Add Authentication classes as needed
handle(); //serve the response
By default Restler runs in debug mode more fine tuned for API developer, by showing detailed error messages and prettifying the api result to human readbale form
By turning on production mode you will gain some performance boost as it will cache the routes (comment parsing happens only once instead of every api call), few other files and avoid giving out debug information
p
ire_once '../../../vendor/restler.php';
Luracast\Restler\Restler;
tup restler
new Restler(true); //turn on production mode by passing true.
you are using file based cache (the default) make sure cache folder is
itable. when you make changes to your code make sure you delete the
outes.php inside the cache folder
.
$reques_data
in api method parameters and getRequestData()
on restler
instance now excludes $_GET
parameters.Defaults::$emptyBodyForNullResponse
to false.composer install --dev
Resources::$excludedHttpMethods
(array)Resources::$excludedPaths
(array)Defaults
RateLimit
class added to rate limit the api usageRestler 3.0 is completely rewritten from Restler 2.0 with best practices in mind for
Restler 3.0
$r->setCompatibilityMode($version);
@access hybrid
PHPDoc commentsuppress_response_codes
as query string, when set to true;
all http responses will be returned with HTTP OK with the errors in the
body to accommodate mobile and less privileged clients.CommentParser
which adds support for embedded data in multiple
formats{@name value}
Defaults
class with static properties that can be changed to suit the
needs__isAllowed
method instead of __isAuthenticated
so that same
class can be used for Authentication or FilteringRestler 2.0 is a major rewrite to use convention over configuration and it is optimized for performance. Here are some of the major changes and improvements
gateway\classname\methodname\param1\...
)$restler->addAPIClass
it will be mapped to the class name instead of mapping
it to the root