phprs\Bootstrap::run PHP Method

run() public static method

public static run ( $conf_file )
    public static function run($conf_file)
    {
        $err = null;
        $protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';
        //执行请求
        try {
            require_once __DIR__ . '/AutoLoad.php';
            $factory = new IoCFactory($conf_file);
            $router = $factory->create('phprs\\RouterWithCache');
            $router();
        } catch (NotFound $e) {
            header($protocol . ' 404 Not Found');
            $err = $e;
        } catch (BadRequest $e) {
            header($protocol . ' 400 Bad Request');
            $err = $e;
        } catch (Forbidden $e) {
            header($protocol . ' 403 Forbidden');
            $err = $e;
        } catch (AuthenticationTimeout $e) {
            header($protocol . ' 419 Authentication Timeout');
            $err = $e;
        } catch (ExceptionWithHttpStatus $e) {
            header($protocol . ' ' . $e->status);
            $err = $e;
        } catch (\Exception $e) {
            header($protocol . ' 500 Internal Server Error');
            $err = $e;
        }
        if ($err) {
            header("Content-Type: application/json; charset=UTF-8");
            $estr = array('error' => get_class($err), 'message' => $err->getMessage());
            echo json_encode($estr);
        }
    }

Usage Example

コード例 #1
0
ファイル: index.php プロジェクト: caoym/phprs-restful
<?php

use phprs\Bootstrap;
use phprs\util\Logger;
//ini_set('display_errors', 0);
// ** if using composer, disable the following line **
require_once __DIR__ . '/../../lib/phprs/AutoLoad.php';
// ** if using composer, enable the following line **
//require_once __DIR__."/../vendor/autoload.php";
//set log flag
//Logger::$flags = Logger::WARNING|Logger::DEBUG|Logger::ERROR|Logger::INFO;
//set log output
//Logger::$writer = Logger::$to_echo;
//simulate request in CLI
//$_SERVER['REQUEST_URI'] = '/api/apis';
//$_SERVER['REQUEST_METHOD'] = 'GET';
Bootstrap::run(__DIR__ . '/../conf.php');
Bootstrap