yii\web\Request::getScriptUrl PHP Method

getScriptUrl() public method

The implementation of this method referenced Zend_Controller_Request_Http in Zend Framework.
public getScriptUrl ( ) : string
return string the relative URL of the entry script.
    public function getScriptUrl()
    {
        if ($this->_scriptUrl === null) {
            $scriptFile = $this->getScriptFile();
            $scriptName = basename($scriptFile);
            if (isset($_SERVER['SCRIPT_NAME']) && basename($_SERVER['SCRIPT_NAME']) === $scriptName) {
                $this->_scriptUrl = $_SERVER['SCRIPT_NAME'];
            } elseif (isset($_SERVER['PHP_SELF']) && basename($_SERVER['PHP_SELF']) === $scriptName) {
                $this->_scriptUrl = $_SERVER['PHP_SELF'];
            } elseif (isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME']) === $scriptName) {
                $this->_scriptUrl = $_SERVER['ORIG_SCRIPT_NAME'];
            } elseif (isset($_SERVER['PHP_SELF']) && ($pos = strpos($_SERVER['PHP_SELF'], '/' . $scriptName)) !== false) {
                $this->_scriptUrl = substr($_SERVER['SCRIPT_NAME'], 0, $pos) . '/' . $scriptName;
            } elseif (!empty($_SERVER['DOCUMENT_ROOT']) && strpos($scriptFile, $_SERVER['DOCUMENT_ROOT']) === 0) {
                $this->_scriptUrl = str_replace('\\', '/', str_replace($_SERVER['DOCUMENT_ROOT'], '', $scriptFile));
            } else {
                throw new InvalidConfigException('Unable to determine the entry script URL.');
            }
        }
        return $this->_scriptUrl;
    }

Usage Example

 /**
  * Redirect to the current URL with given language code applied
  *
  * @param string $language the language code to add. Can also be empty to not add any language code.
  */
 protected function redirectToLanguage($language)
 {
     // Examples:
     // 1) /baseurl/index.php/some/page?q=foo
     // 2) /baseurl/some/page?q=foo
     // 3)
     // 4) /some/page?q=foo
     if ($this->showScriptName) {
         // 1) /baseurl/index.php
         // 2) /baseurl/index.php
         // 3) /index.php
         // 4) /index.php
         $redirectUrl = $this->_request->getScriptUrl();
     } else {
         // 1) /baseurl
         // 2) /baseurl
         // 3)
         // 4)
         $redirectUrl = $this->_request->getBaseUrl();
     }
     if ($language) {
         $redirectUrl .= '/' . $language;
     }
     // 1) some/page
     // 2) some/page
     // 3)
     // 4) some/page
     $pathInfo = $this->_request->getPathInfo();
     if ($pathInfo) {
         $redirectUrl .= '/' . $pathInfo;
     }
     if ($redirectUrl === '') {
         $redirectUrl = '/';
     }
     // 1) q=foo
     // 2) q=foo
     // 3)
     // 4) q=foo
     $queryString = $this->_request->getQueryString();
     if ($queryString) {
         $redirectUrl .= '?' . $queryString;
     }
     Yii::$app->getResponse()->redirect($redirectUrl);
     if (YII_ENV_TEST) {
         throw new \yii\base\Exception(\yii\helpers\Url::to($redirectUrl));
     } else {
         Yii::$app->end();
     }
 }
All Usage Examples Of yii\web\Request::getScriptUrl