PHP code example of dawaa / jacwright-restserver-extended
1. Go to this page and download the library: Download dawaa/jacwright-restserver-extended library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
dawaa / jacwright-restserver-extended example snippets
// index.php
// Require the autoload.php generated by Composer.
Start it
$server->start();
// index.php
// Require the autoload.php generated by Composer.
// Configure the server Resources/ directory
$server->configure(
array(
'resourcesPath' => __DIR__ . '/relative/path/to/resources/newNameForResources',
// 'configPath' => __DIR__ . '/relative/path/to/config/whateverNameYouWant.php' // ignoring this for now.
)
);
// Start it
$server->start();
// index.php
// Require the autoload.php generated by Composer.
// Configure the server Resources/ directory
$server->configure(
array(
'resourcesPath' => __DIR__ . '/relative/path/to/resources/newNameForResources',
'configPath' => __DIR__ . '/relative/path/to/config/whateverNameYouWant.php'
)
);
// Start it
$server->start();
// ./config.php
$config = array(
'db_user' => 'DATABASE_USERNAME', // ME', // 'charset' => 'utf8' // by default it's utf8
);
// Note the return here, muy importante amigo!
return $config;
// ./src/config.php
$config = array(
'db_user' => 'DATABASE_USERNAME', // // 'charset' => 'utf8' // by default it's utf8
);
// Note the return here, muy importante amigo!
return $config;
// ./Resources/Users/Users.php
namespace Resources\Users;
use DAwaa\Core\Controller;
class Users extends Controller {
/**
* @url GET /
*/
public function fetchAll() {
$users = $this->model->fetchUsers();
return $this->respondWith( $users );
}
}
// ./Resources/Users/Users.php
namespace Resources\Users;
use DAwaa\Core\Controller;
class Users extends Controller {
/**
* @url GET /
*/
public function fetchAll() { // .. code }
/**
* @url GET /activity/$userId
*/
public function fetchUserActivity($userId) {
$activity = $this->model->fetchActivity( $userId );
return $this->respondWith( $activity );
}
}
// ./Resources/Users/Users.php
namespace Resources\Users;
use DAwaa\Core\Controller;
class Users extends Controller {
/**
* @url GET /$userId
*/
public function fetchUsers($userId) { // renamed from fetchAll -> fetchUsers
if ( $userId === null ) {
$response = $this->model->fetchUsers();
} else {
$response = $this->model->fetchUser( $userId );
}
return $this->respondWith( $response );
}
/**
* @url GET /activity/$userId
*/
public function fetchUserActivity($userId) { // .. code }
}
// ./Resources/Users/Users.php
namespace Resources\Users;
use DAwaa\Core\Controller;
class Users extends Controller {
public function init() {
// .. code here will be run before anything else
}
/**
* @url GET /$userId
*/
public function fetchUsers($userId) {
if ( $userId === null ) {
$response = $this->model->fetchUsers();
} else {
$response = $this->model->fetchUser( $userId );
}
return $this->respondWith( $response );
}
}
// ./Resources/Users/Users.php
namespace Resources\Users;
use DAwaa\Core\Controller;
class Users extends Controller {
/**
* @url GET /$userId
*/
public function fetchUsers($userId) {
if ( $userId === null ) {
$response = $this->model->fetchUsers();
} else {
$response = $this->model->fetchUser( $userId );
}
return $this->respondWith( $response );
}
}
// ./Resources/Users/Users.php
namespace Resources\Users;
use DAwaa\Core\Controller;
class Users extends Controller {
/**
* @url GET /
* @expand addExtra addExtraUnnecessaryInformation
*/
public function fetchUsers() {
$users = $this->model->fetchUsers();
return $this->respondWith( $users );
}
public function addExtraUnnecessaryInformation() {
$unnecessaryInformation = array(
'isANerd' => true
);
return $unnecessaryInformation;
}
}
// ./Resources/Users/Users.php
namespace Resources\Users;
use DAwaa\Core\Controller;
class Users extends Controller {
/**
* @url GET /
* @expand addExtra addExtraUnnecessaryInformation
* @expand friends getUsersFriends
* @unique id
*/
public function fetchUsers() {
$users = $this->model->fetchUsers();
return $this->respondWith( $users );
}
public function addExtraUnnecessaryInformation() {
$unnecessaryInformation = array(
'isANerd' => true
);
return $unnecessaryInformation;
}
}
// ./Resources/Users/Users.php
namespace Resources\Users;
use DAwaa\Core\Controller;
class Users extends Controller {
/**
* @url GET /
* @expand addExtra addExtraUnnecessaryInformation
* @expand friends getUsersFriends
* @unique id
*/
public function fetchUsers() {
$users = $this->model->fetchUsers();
return $this->respondWith( $users );
}
public function getUsersFriends($userId) {
$response = array();
$friends = $this->model->getFriendsById( $userId );
// Beautify the response somewhat.
foreach ( $friends as $friend ) {
$friendId = $friend[ 'id' ];
$response[ $friendId ] = $friend;
}
return $response;
}
public function addExtraUnnecessaryInformation() {
$unnecessaryInformation = array(
'isANerd' => true
);
return $unnecessaryInformation;
}
}
// ./Resources/Users/Users.php
namespace Resources\Users;
use DAwaa\Core\Controller;
/**
* @model \Provide\Full\Namespace\To\Other\Class
*/
class Users extends Controller {
// .. code
}
// ./Resources/Users/Model/UsersModel.php
namespace Resources\Users\Model;
use DAwaa\Core\Model;
class UsersModel extends Model {
public function fetchUsers() {
// You can either directly write your queries like this
$users = $this->query( 'select * from users' )->result_array();
// Or make use of static variables inside classes to allow for some
// sort of namespaceing to easier structure bigger applications (imho..)
$sql = Statements\Select::$all;
$users = $this->query( $sql )->result_array();
return $users;
}
}
namespace Resources\Users\Model\Statements;
class Select {
public static $all =
"
select
*
from
users
";
}
// ./Resources/Users/Model/UsersModel.php
namespace Resources\Users\Model;
use DAwaa\Core\Model;
class UsersModel extends Model {
public function init() {
// .. code here will be run before anything else
}
public function fetchUsers() {
// You can either directly write your queries like this
$users = $this->query( 'select * from users' )->result_array();
// Or make use of static variables inside classes to allow for some
// sort of namespaceing to easier structure bigger applications (imho..)
$sql = Statements\Select::$all;
$users = $this->query( $sql )->result_array();
return $users;
}
}