Exemplo n.º 1
0
<?php

require_once __DIR__ . '/../vendor/autoload.php';
\ByJG\RestServer\RouteHandler::handleRoute();
Exemplo n.º 2
0
 /**
  * Process the ROUTE (see httpdocs/route-dist.php)
  *
  * ModuleAlias needs to be an array like:
  *  [ 'alias' => 'Full.Namespace.To.Class' ]
  *
  * RoutePattern needs to be an array like:
  * [
  *     [
  *         "method" => ['GET'],
  *         "pattern" => '/{version}/{module}/{action}/{id:[0-9]+}/{secondid}.{output}',
  *         "handler" => '\ByJG\RestServer\ServiceHandler'
  *    ],
  * ]
  *
  * @param array $moduleAlias
  * @param array $routePattern
  * @param string $version
  * @param string $defaultOutput
  * @param string $routeIndex
  */
 public static function handleRoute($moduleAlias = [], $routePattern = null, $version = '1.0', $defaultOutput = null, $routeIndex = "index.php")
 {
     ob_start();
     session_start();
     /**
      * @var RouteHandler
      */
     $route = RouteHandler::getInstance();
     /**
      * Module Alias contains the alias for full namespace class.
      *
      * For example, instead to request:
      * http://somehost/module/Full.NameSpace.To.Module
      *
      * you can request only:
      * http://somehost/module/somealias
      */
     foreach ((array) $moduleAlias as $alias => $module) {
         $route->addModuleAlias($alias, $module);
     }
     /**
      * You can create RESTFul compliant URL by adding the version.
      *
      * In the route pattern:
      * /{version}/someurl
      *
      * Setting the value here XMLNuke route will automatically replace it.
      *
      * The default value is "1.0"
      */
     $route->setDefaultRestVersion($version);
     /**
      * You can set the defaultOutput where is not necessary to set the output in the URL
      */
     $route->setDefaultOutput($defaultOutput);
     /**
      * There are a couple of basic routes pattern for the default parameters
      *
      * e.g.
      *
      * /1.0/command/1.json
      * /1.0/command/1.xml
      *
      * You can create your own route pattern by define the methods here
      */
     if (!empty($routePattern)) {
         $route->setDefaultMethods($routePattern);
     }
     // --------------------------------------------------------------------------
     // You do not need change from this point
     // --------------------------------------------------------------------------
     if (!empty($_SERVER['SCRIPT_FILENAME']) && file_exists($_SERVER['SCRIPT_FILENAME']) && basename($_SERVER['SCRIPT_FILENAME']) !== "route.php" && basename($_SERVER['SCRIPT_FILENAME']) !== $routeIndex) {
         $file = $_SERVER['SCRIPT_FILENAME'];
         if (strpos($file, '.php') !== false) {
             require_once $file;
         } else {
             header("Content-Type: " . RouteHandler::mimeContentType($file));
             echo file_get_contents($file);
         }
         return;
     }
     $route->process();
 }