示例#1
0
[![Forms](../resources/Form.gif)](users.html)
We have made it easy to try different styles
Also this example serves as an example for our Blade template integration

See bootstrap3.blade.php and foundation5.blade.php

Content:

*[bootstrap3.blade.php]: _016_forms/views/base/bootstrap3.blade.php
*[foundation5.blade.php]: _016_forms/views/base/foundation5.blade.php
*/
$loader = (include '../../../vendor/autoload.php');
$loader->setUseIncludePath(true);
use Luracast\Restler\Data\Validator;
use Luracast\Restler\Restler;
use Luracast\Restler\Defaults;
use Luracast\Restler\Format\HtmlFormat;
use Luracast\Restler\UI\Forms;
use Luracast\Restler\UI\FormStyles;
HtmlFormat::$viewPath = __DIR__ . '/views';
HtmlFormat::$template = 'blade';
Validator::$holdException = true;
$themes = array('amelia', 'cerulean', 'cosmo', 'cyborg', 'darkly', 'flatly', 'journal', 'lumen', 'readable', 'simplex', 'slate', 'spacelab', 'superhero', 'united', 'yeti');
$theme = isset($_GET['theme']) ? $_GET['theme'] : $themes[array_rand($themes, 1)];
$style = $theme == 'foundation5' ? 'foundation5' : 'bootstrap3';
HtmlFormat::$data += compact('theme', 'themes', 'style');
Forms::$style = FormStyles::${$style};
$r = new Restler();
$r->setSupportedFormats('HtmlFormat');
$r->addAPIClass('Users');
$r->handle();
示例#2
0
 /**
  * Stage 2: Users response is recorded by the server
  *
  * Server redirects the user back with the result.
  *
  * If successful,
  *
  * Client exchanges the authorization code by a direct call (not through
  * user's browser) to get access token which can then be used call protected
  * APIs, if completed it calls a protected api and displays the result
  * otherwise client ends up showing the error message
  *
  * Else
  *
  * Client renders the error message to the user
  *
  * @param string $code
  * @param string $error_description
  * @param string $error_uri
  *
  * @return array
  *
  * @format HtmlFormat
  */
 public function authorized($code = null, $error_description = null, $error_uri = null)
 {
     // the user denied the authorization request
     if (!$code) {
         HtmlFormat::$view = 'oauth2/client/denied.twig';
         return array('error' => compact('error_description', 'error_uri'));
     }
     // exchange authorization code for access token
     $query = array('grant_type' => 'authorization_code', 'code' => $code, 'client_id' => self::$clientId, 'client_secret' => self::$clientSecret, 'redirect_uri' => self::$authorizeRedirectUrl);
     //call the API using cURL
     $curl = new Curl();
     $endpoint = self::$serverUrl . '/grant';
     $response = $curl->request($endpoint, $query, 'POST');
     if (!json_decode($response['response'], true)) {
         $status = $response['headers']['http_code'];
         echo '<h1>something went wrong - see the raw response</h1>';
         echo '<h2> Http ' . $status . ' - ' . RestException::$codes[$status] . '</h2>';
         exit('<pre>' . print_r($response, true) . '</pre>');
     }
     $error = array();
     $response = json_decode($response['response'], true);
     // render error if applicable
     ($error['error_description'] = Util::nestedValue($response, 'error_description')) || ($error['error_description'] = Util::nestedValue($response, 'error', 'message')) || ($error['error_description'] = Util::nestedValue($response, 'errorMessage')) || ($error['error_description'] = Util::nestedValue($response, 'errorNumber')) || ($error['error_description'] = 'Unknown Error');
     $error_uri = Util::nestedValue($response, 'error_uri');
     if ($error_uri) {
         $error['error_uri'] = $error_uri;
     }
     // if it is successful, call the API with the retrieved token
     if ($token = Util::nestedValue($response, 'access_token')) {
         // make request to the API for awesome data
         $endpoint = self::$serverUrl . '/access?access_token=' . $token;
         $response = $curl->request($endpoint);
         HtmlFormat::$view = 'oauth2/client/granted.twig';
         return array('token' => $token, 'endpoint' => $endpoint) + json_decode($response['response'], true);
     }
     HtmlFormat::$view = 'oauth2/client/error.twig';
     return array('error' => $error);
 }