Trabla: Luracast Restler API Framework: quick start
Tutorial for Windows and XAMPP
Solving:
1. Install composer
2. In XAMPP create folder for api
Example:
I've created
C:\xampp\htdocs\mysite\api
3. Run CMD (windows command line ) change directory to your api
Example:
CD C:\xampp\htdocs\mysite\api
4. Type command composer init in CMD
Example:
C:\xampp\htdocs\mysite\api>composer init
Welcome to the Composer config generator
This command will guide you through creating your composer.json config.
Package name (<vendor>/<name>) [SamuraiKit/api]: mysite/api
Description []: My Site Api
Author []: Samurai Kit
Invalid author string. Must be in the format: John Smith <john@example.com>
Author []: Samurai Kit <mysite@gmail.com>
Minimum Stability []: dev
Package Type []: private
License []: proprietary
Define your dependencies.
Would you like to define your dependencies (require) interactively [yes]? no
Would you like to define your dev dependencies (require-dev) interactively [yes]
? no
{
"name": "mysite/api",
"description": "My Site Api",
"type": "private",
"license": "proprietary",
"authors": [
{
"name": "Samurai Kit",
"email": "mysite@gmail.com"
}
],
"minimum-stability": "dev",
"require": {}
}
Do you confirm generation [yes]? yes
C:\xampp\htdocs\mysite\api>
5. Run composer command to install Restler Framework
composer require restler/framework:3.0.0-RC6
C:\xampp\htdocs\mysite\api>composer require restler/framework:3.0.0-RC6
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Installing restler/framework (3.0.0-RC6)
Downloading: 100%
restler/framework suggests installing rodneyrehm/plist (If you need Apple plist
binary/xml format)
restler/framework suggests installing zendframework/zendamf (If you need AMF for
mat)
restler/framework suggests installing symfony/yaml (If you need YAML format)
restler/framework suggests installing twig/twig (If you want to use twig templat
es with Html format)
restler/framework suggests installing mustache/mustache (If you want to use must
ache/handlebar templates with Html format)
restler/framework suggests installing illuminate/view (If you want to use larave
l blade templates with Html format)
restler/framework suggests installing bshaffer/oauth2-server-php (If you want to
use OAuth2 for authentication)
Writing lock file
Generating autoload files
6. Run composer command to install php-activerecord
php-activerecord is an open source ORM library based on the ActiveRecord pattern.
C:\xampp\htdocs\mysite\api>composer require php-activerecord/php-activerecord:
v1.1.2
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Installing php-activerecord/php-activerecord (v1.1.2)
Downloading: 100%
Writing lock file
Generating autoload files
6. Now create following folder structure for Api Classes and DB Models
in api folder
Example:
C:\xampp\htdocs\mysite\api\src\
C:\xampp\htdocs\mysite\api\src\class\
C:\xampp\htdocs\mysite\api\src\class\v1\
C:\xampp\htdocs\mysite\api\src\models\
7. Create empty "cache" folder in Api folder.
After deploy on server set chmod 777.
Example:
C:\xampp\htdocs\mysite\api\cache\
8. Create .htaccess file in api folder with following content:
Example file location: C:\xampp\htdocs\mysite\api\.htaccess
Header always set Access-Control-Allow-Origin "*"
Header always set Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept"
Header add Access-Control-Allow-Methods: "GET,POST,OPTIONS,DELETE,PUT"
Options -MultiViews
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ index.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^(.*)$ $1 [R=200,L]
</IfModule>
9. Create file index.php in api folder with following content:
Example file location: C:\xampp\htdocs\mysite\api\index.php
<?php
//===========================================================================
// Configuration
//===========================================================================
date_default_timezone_set('UTC');
define('API_ROOT', __DIR__ );
define('PROD_MODE', FALSE );
define('API_VERSION',1);
define('DEFAULT_DB_CONN','DEV');
//Database connection params
$db_connections = array(
//PostgreSQL
'DEV' => 'pgsql://username:password@localhost/database_name',
//MySQL
'MYSQL' => 'mysql://username:password@localhost/database_name',
);
require_once API_ROOT.'/vendor/autoload.php';
// Active Record Config
$cfg = ActiveRecord\Config::instance();
$cfg->set_model_directory( API_ROOT.'/src/models');
$cfg->set_connections(
$db_connections
);
ActiveRecord\Config::initialize(function($cfg)
{
$cfg->set_default_connection( DEFAULT_DB_CONN );
});
//===========================================================================
// / Configuration
//===========================================================================
use Luracast\Restler\Restler;
use Luracast\Restler\Defaults;
//If prod mode - false , debug logs are shown
$r = new Restler( PROD_MODE );
$r->setAPIVersion( API_VERSION );
$r->addAPIClass('User'); // Not implemented yet
$r->addAPIClass('Explorer');
$r->setSupportedFormats(
'JsonFormat'
);
$r->handle();
10. Now lets create APi class User -
create file User.php in following folder:
C:\xampp\htdocs\mysite\api\src\class\v1\User.php
<?php
namespace v1;
use Luracast\Restler\RestException;
use stdClass;
use ActiveRecord\JsonSerializer;
class User
{
/**
* returns user
* @param string $token
* @param string $name
* @param int $age
*
* @return (string)
*/
function get( $token, $name, $age = 15 )
{
if($token === 'SuperSecretToken'){
$user = new stdClass();
$user->name = $name;
$user->age = $age;
return $user;
}else{
throw new RestException(404,'Token Not Found');
}
}
/**
* save user to db
*
* @param string $token {@from query}
* @param string $name {@from query}
* @param array $data {@from body} JSON array of user data
*/
function post( $token, $name, $data ){
if( $token === 'SuperSecretToken' ){
throw new RestException(400,'Not Implemented');
}else{
throw new RestException(404,'Token Not Found');
}
}
}
11. Edit composer json file in api folder , add
"autoload":{
"psr-4":{
"v1\\":"src/class/v1/"
}
}
Example:
{
"name": "mysite/api",
"description": "My Site Api",
"type": "private",
"license": "proprietary",
"authors": [
{
"name": "Samurai Kit",
"email": "mysite@gmail.com"
}
],
"minimum-stability": "dev",
"require": {
"restler/framework": "3.0.0-RC6",
"php-activerecord/php-activerecord": "v1.1.2"
},
"autoload":{
"psr-4":{
"v1\\":"src/class/v1/"
}
}
}
12. Run CMD , goto api folder and type composer update
Example:
C:\Users\Alex>cd C:\xampp\htdocs\mysite\api
C:\xampp\htdocs\mysite\api>composer update
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Writing lock file
Generating autoload files
C:\xampp\htdocs\mysite\api>
13. Run command:
composer update --optimize-autoloader
Example:
C:\xampp\htdocs\mysite\api>composer update --optimize-autoloader
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating optimized autoload files
C:\xampp\htdocs\mysite\api>
14. Start Apache in XAMPP control panel
15. Open browser and goto api page:
Example:
http://localhost/mysite/api/explorer/
Result:
ATTENTION: before deploy run command in Api folder ( creates class pathes for faster loading )
composer update --optimize-autoloader
Note:
1. Luracast Restler API documentation:
http://restler3.luracast.com/examples/_008_documentation/readme.html
2. php-activerecord ORM documentation: http://www.phpactiverecord.org/
No comments:
Post a Comment