SimpleSAML\Utils\HTTP::getFirstPathElement PHP Method

getFirstPathElement() public static method

Retrieve the first element of the URL path.
Author: Andreas Solberg, UNINETT AS ([email protected])
public static getFirstPathElement ( boolean $trailingslash = true ) : string
$trailingslash boolean Whether to add a trailing slash to the element or not. Defaults to true.
return string The first element of the URL path, with an optional, trailing slash.
    public static function getFirstPathElement($trailingslash = true)
    {
        if (preg_match('|^/(.*?)/|', $_SERVER['SCRIPT_NAME'], $matches)) {
            return ($trailingslash ? '/' : '') . $matches[1];
        }
        return '';
    }

Usage Example

Example #1
0
 /**
  * Retrieve the absolute path of the SimpleSAMLphp installation, relative to the root of the website.
  *
  * For example: simplesaml/
  *
  * The path will always end with a '/' and never have a leading slash.
  *
  * @return string The absolute path relative to the root of the website.
  *
  * @throws SimpleSAML_Error_Exception If the format of 'baseurlpath' is incorrect.
  */
 public function getBaseURL()
 {
     $baseURL = $this->getString('baseurlpath', 'simplesaml/');
     if (preg_match('/^\\*(.*)$/D', $baseURL, $matches)) {
         // deprecated behaviour, will be removed in the future
         return \SimpleSAML\Utils\HTTP::getFirstPathElement(false) . $matches[1];
     }
     if (preg_match('#^https?://[^/]*/(.*)$#', $baseURL, $matches)) {
         // we have a full url, we need to strip the path
         return $matches[1];
     } elseif ($baseURL === '' || $baseURL === '/') {
         // Root directory of site
         return '';
     } elseif (preg_match('#^/?([^/]?.*/)#D', $baseURL, $matches)) {
         // local path only
         return $matches[1];
     } else {
         // invalid format
         throw new SimpleSAML_Error_Exception('Incorrect format for option \'baseurlpath\'. Value is: "' . $this->getString('baseurlpath', 'simplesaml/') . '". Valid format is in the form' . ' [(http|https)://(hostname|fqdn)[:port]]/[path/to/simplesaml/].');
     }
 }
All Usage Examples Of SimpleSAML\Utils\HTTP::getFirstPathElement