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;
    }

}

$userId = '1234';
$sql    = 'select * from users where id = :0';
$user   = $this->query( $sql, [ $userId ] )->row();

var_dump( $user );
=>
// object(stdClass) {
//     ["id"] => int(1234)
//     ["username"] => string(4) "Test"
// }
var_dump ( $user->username );
=>
// Test


$userId = '1234';
$sql    = 'select * from users where id = :0';
$user   = $this->query( $sql, [ $userId ] )->row_array();

var_dump( $user );
=>
// array(2) {
//     ["id"] => int(1234)
//     ["username"] => string(4) "Test"
// }
var_dump ( $user["username"] );
=>
// Test

$userId   = '1234';
$sql      = 'select username from users where id = :0';
$username = $this->query( $sql, [ $userId ] )->row_result();

var_dump( $username );
=>
// Test

$sql   = 'select * from users limit 2';
$users = $this->query( $sql )->result();

var_dump( $users );
=>
// array(2) {
//    [0] => object(stdClass) {
//        ["id"] => string(4) "1234"
//        ["username"] => string(4) "Test"
//    }
//    [1] => object(stdClass) {
//        ["id"] => string(5) "12345"
//        ["username"] => string(5) "Jesus"
//    }
// }

var_dump( $users[0]->id );
=>
// 1234

$sql = 'select * from users limit 2';
$users = $this->query( $sql )->result_array();

var_dump( $users );
=>
// array(2) {
//    [0] => array(2) {
//        ["id"] => string(4) "1234"
//        ["username"] => string(4) "Test"
//    }
//    [1] => array(2) {
//        ["id"] => string(5) "12345"
//        ["username"] => string(5) "Jesus"
//    }
// }

var_dump( $users[0]["id"] );
=>
// 1234

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_URI} !v1/(.*)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* http://%{HTTP_HOST}/v1/ [QSA]
RewriteRule ^v1/(.*)/?$ index.php?segments=$1 [QSA,L]
bash
jacwright-restserver-extended/Resources/
`- Users/
   |- Model/
   |  |- Statements/          # example structure and files..
   |  |  |- Select.php
   |  |  |- Delete.php
   |  |  `- Update.php
   |  `- UsersModel.php
   `- Users.php