Prado\Web\THttpRequest::init PHP 메소드

init() 공개 메소드

This method is required by IModule and is invoked by application.
public init ( $config )
    public function init($config)
    {
        // Fill in default request info when the script is run in command line
        if (php_sapi_name() === 'cli') {
            $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
            $_SERVER['REQUEST_METHOD'] = 'GET';
            $_SERVER['SERVER_NAME'] = 'localhost';
            $_SERVER['SERVER_PORT'] = 80;
            $_SERVER['HTTP_USER_AGENT'] = '';
        }
        // Info about server variables:
        // PHP_SELF contains real URI (w/ path info, w/o query string)
        // SCRIPT_NAME is the real URI for the requested script (w/o path info and query string)
        // QUERY_STRING is the string following the '?' in the ur (eg the a=x part in http://foo/bar?a=x)
        // REQUEST_URI contains the URI part entered in the browser address bar
        // SCRIPT_FILENAME is the file path to the executing script
        if (isset($_SERVER['REQUEST_URI'])) {
            $this->_requestUri = $_SERVER['REQUEST_URI'];
        } else {
            // TBD: in this case, SCRIPT_NAME need to be escaped
            $this->_requestUri = $_SERVER['SCRIPT_NAME'] . (empty($_SERVER['QUERY_STRING']) ? '' : '?' . $_SERVER['QUERY_STRING']);
        }
        if ($this->_cgiFix & self::CGIFIX__PATH_INFO && isset($_SERVER['ORIG_PATH_INFO'])) {
            $this->_pathInfo = substr($_SERVER['ORIG_PATH_INFO'], strlen($_SERVER['SCRIPT_NAME']));
        } elseif (isset($_SERVER['PATH_INFO'])) {
            $this->_pathInfo = $_SERVER['PATH_INFO'];
        } else {
            if (strpos($_SERVER['PHP_SELF'], $_SERVER['SCRIPT_NAME']) === 0 && $_SERVER['PHP_SELF'] !== $_SERVER['SCRIPT_NAME']) {
                $this->_pathInfo = substr($_SERVER['PHP_SELF'], strlen($_SERVER['SCRIPT_NAME']));
            } else {
                $this->_pathInfo = '';
            }
        }
        if (get_magic_quotes_gpc()) {
            if (isset($_GET)) {
                $_GET = $this->stripSlashes($_GET);
            }
            if (isset($_POST)) {
                $_POST = $this->stripSlashes($_POST);
            }
            if (isset($_REQUEST)) {
                $_REQUEST = $this->stripSlashes($_REQUEST);
            }
            if (isset($_COOKIE)) {
                $_COOKIE = $this->stripSlashes($_COOKIE);
            }
        }
        $this->getApplication()->setRequest($this);
    }

Usage Example

예제 #1
0
 public function testRequestWithUrlMapping()
 {
     Prado::Using('System.Web.TUrlMapping');
     $confstr = '<config><url ServiceId="testService" ServiceParameter="testServiceParam" pattern="test/{param}/?" parameters.param="\\w+"/></config>';
     $config = new TXmlDocument('1.0', 'utf8');
     $config->loadFromString($confstr);
     $module = new TUrlMapping();
     self::$app->setModule('friendly-url', $module);
     if (isset($_GET['page'])) {
         unset($_GET['page']);
     }
     // Remove service from a previous test !
     $_SERVER['REQUEST_URI'] = '/index.php/test/value2';
     $_SERVER['SCRIPT_NAME'] = '/index.php';
     $_SERVER['PHP_SELF'] = '/index.php/test/value2';
     $_SERVER['QUERY_STRING'] = '';
     $_SERVER['PATH_INFO'] = '/test/value2';
     $request = new THttpRequest();
     $request->setUrlManager('friendly-url');
     $request->setUrlFormat(THttpRequestUrlFormat::Path);
     $request->init(null);
     $module->init($config);
     self::assertEquals('testService', $request->resolveRequest(array('page', 'testService')));
 }