PHP Classes

File: documentation/en/library/URI.md

Recommend this page to a friend!
  Classes of Fernando Val   Springy   documentation/en/library/URI.md   Download  
File: documentation/en/library/URI.md
Role: Auxiliary data
Content type: text/markdown
Description: Auxiliary data
Class: Springy
Microframework for Web application development
Author: By
Last change: Some corrections
Merge pull request #9 from fernandoval/development

v3.6.1
Date: 5 years ago
Size: 11,801 bytes
 

Contents

Class file image Download

URI

About

URI is a static class used by the framework to translate and construct URL and URI and query strings.

Properties

The class has no properties you can access directly.

|Property|Type|Description| |---|---|---| |$uri_string|private static|The current URI| |$segments|private static|An array of the URI segments| |$ignored_segments|private static|An array of the URI segments ignored by the framework to translate de controller| |$get_params|private static|An array of the variables received in the query string passed by HTTP GET method| |$segment_page|private static|The index of the segment which determines the current page| |$class_controller|private static|The name of the controller class|

Methods

|Name|Type|Description| |---|---|---| |_fetch_uri_string()|private static|Get the URI string.| |_set_class_controller()|private static|Define the name of the controller class.| |parseURI()|public static|Translate the URI in segments and query string variables. This method is used by the framework starter to determine the controller which is be called.| |validateURI()|public static|Validate the segments quantity for the current controller.| |getControllerClass()|public static|Return the name of the controller class.| |getURIString()|public static|Return the URI string.| |currentPage()|public static|Return the content of the segment which represent the current page.| |relativePathPage()|public static|Return a string with the relative path to the current page.| |currentPageURI()|public static|Return a string with the path URL the current page (without the protocol).| |setCurrentPage()|public static|Define the segment of the current page.| |getSegment()|public static|Get any segment of the URI.| |getIgnoredSegment()|public static|Get any ignored segment of the URI.| |getAllSegments()|public static|Return the array of segments.| |getAllIgnoredSegments()|public static|Return the array of ignored segments.| |addSegment()|public static|Add a segment to the end of segments array.| |insertSegment()|public static|Insert a segment in any position of the segments array.| |_GET()|public static|Return the value of a query string variable.| |getParam()|public static|An alias to _GET() method.| |getParams()|public static|Return the array of query string variables.| |requestMethod()|public static|Return the request method string.| |removeParam()|public static|Remove a variable from the array of query string variables.| |setParam()|public static|Set value to a query string parameter.| |buildURL()|public static|Return the string of an URI with the received parameters.| |httpHost()|public static|Return the current host with protocol.| |_host()|private static|Return an URL host with protocolo.| |encode_param()|private static|Enconde an array of parameters into a query string.| |redirect()|public static|Set a redirect status header and finish the application.| |makeSlug()|public static|Generate a slug, removing the accented and special characters from a string and convert spaces into minus symbol.| |isAjaxRequest()|public static|Return true if is an XML HTTL request.|

Methods Details

parseURI()

This method is invoked by the framework starter to initiate the internal properties. Do not call it in your application.

    public static function parseURI()

validateURI()

This method performs the prevalidation to the current controller.

It count the number of segments and validate against the uri configuration.

    public static function validateURI()

getControllerClass()

This method return the name of the controller class.

    public static function getControllerClass()

Sample:

    $controllerName = Springy\URI::getControllerClass();

getURIString()

Return the current URI string.

    public static function getURIString()

Sample:

    $uri = Springy\URI::getURIString();

currentPage()

Return the content of the segment which represent the current page.

    public static function currentPage()

Sample:

    $page = Springy\URI::currentPage();

relativePathPage()

Return the content of the segment which represent the current page.

    public static function relativePathPage($consider_controller_root = false)

Sample:

    $path = Springy\URI::relativePathPage();

currentPageURI()

Return a string with the path URL the current page (without the protocol).

    public static function currentPageURI()

Sample:

    $uri = Springy\URI::currentPageURI();

setCurrentPage()

Define the segment of the current page.

    public static function setCurrentPage($segment_num)

The $segment_num parameter is an integer with the number of the segment to fix as current page.

Returns true if exists a $segment_num relative to the current page in the array of segments or false if does not exists.

Sample:

    $wasSet = Springy\URI::setCurrentPage(1);

getSegment()

Get any segment of the URI.

    public static function getSegment($segment_num, $relative_to_page = true, $consider_controller_root = false)

The $segment_num parameter is an integer with the number of the segment desired.

The $relative_to_page parameter is a boolean value to determine if the desired segment is relative to the current page (default) or the begin of the array of segments.

The $consider_controller_root parameter is a boolean value to determine if the number of segments of the root path of contollers must be decremented (true) or not (false = default).

Returns the value of the segment or false if it does not exists.

Sample:

    $segment = Springy\URI::getSegment(0);

getIgnoredSegment()

Get any ignored segment of the URI.

    public static function getIgnoredSegment($segment_num)

The $segment_num parameter is an integer with the number of the segment desired.

Returns the value of the segment or false if it does not exists.

Sample:

    $segment = Springy\URI::getIgnoredSegment(0);

getAllSegments()

Return the array of segments.

    public static function getAllSegments()

Sample:

    $segments = Springy\URI::getAllSegments();

getAllIgnoredSegments()

Return the array of ignored segments.

    public static function getAllIgnoredSegments()

Sample:

    $ignoredSegments = Springy\URI::getAllIgnoredSegments();

addSegment()

Add a segment to the end of segments array.

    public static function addSegment($segment)

The $segment parameter is an string with segment to be added to the end of the array of segments.

Sample:

    Springy\URI::addSegment('test');

insertSegment()

Insert a segment in any position of the segments array.

    public static function insertSegment($position, $segment)

The $position parameter is an integer with the position where the segment must be inserted.

The $segment parameter is an string with segment to be inserted.

Sample:

    Springy\URI::insertSegment(0, 'test');

_GET()

Return the value of a query string variable.

    public static function _GET($var)

The $var parameter is the name of the query string variable desired.

Returns the value of the variable or false if it does not exists.

Sample:

    $var = Springy\URI::_GET('test');

getParam()

Return the value of a query string variable.

Is an alias to _GET() method.

    public static function getParam($var)

The $var parameter is the name of the query string variable desired.

Returns the value of the variable or false if it does not exists.

Sample:

    $var = Springy\URI::getParam('test');

getParams()

Return the array of query string variables.

    public static function getParams()

Returns the array with all query string variables.

Sample:

    $queryString = Springy\URI::getParams();

requestMethod()

Return the request method string.

    public static function requestMethod()

Sample:

    $requestMethod = Springy\URI::requestMethod();

removeParam()

Remove a variable from the array of query string variables.

    public static function removeParam($var)

The $var parameter is the name of the query string variable to be deleted.

Sample:

    Springy\URI::removeParam('test');

setParam()

Set value to a query string parameter.

    public static function setParam($var, $value)

The $var parameter is the name of the query string variable.

The $value parameter is the value to be assigned to the variable.

Sample:

    Springy\URI::setParam('test', 'my value');

buildURL()

Return the string of an URI with the received parameters.

    public static function buildURL($segments = [], $query = [], $forceRewriteforceRewrite = false, $host = 'dynamic', $include_ignores_segments = true)

The $segments parameter is an array with the segments of the URL.

The $query parameter is an array with the query string variables.

The $forceRewrite parameter is a boolean value to define if URI will be writed in URL redirection form (user frendly - SEF) forced or the value of configuration will be used to it.

The $host parameter is an string with the host name configuration (default = 'dynamic').

The $include_ignores_segments parameter is a boolean value to define if URI will receive the ignored segments as prefix (default = true).

Sample:

    $uri = Springy\URI::buildURL(['search-page'], ['q' => 'query']);

httpHost()

Return the current host with protocol.

    public static function httpHost()

Sample:

    $host = Springy\URI::httpHost();

redirect()

Set a redirect status header and finish the application.

This method sends the status header with a URI redirection to the user browser and finish the application execution.

    public static function redirect($url, $header = 302)

The $url parameter is a string with the URI.

The $header parameter is an integer value with the redirection code (302 = permanente is the default, 301 = temporary).

Sample:

    Springy\URI::redirect('https://www.google.com', 301);

makeSlug()

Generate a slug, removing the accented and special characters from a string and convert spaces into minus symbol.

    public static function makeSlug($txt, $space = '-', $accept = '', $lowercase = true)

The $txt parameter is a string with the text to be converted to slug format.

The $space parameter is a string with the character used as word separator. (default = '-')

The $accept parameter is a string with other characters to be added to regular expression of accpted characters is slug.

The $lowercase parameter is a boolean value that determine if the slug will be returned as lowercase string or as is.

Return the slug string.

Sample:

    $slug = Springy\URI::makeSlug('This is a slug');

    // The result is: 'this-is-a-slug'

isAjaxRequest()

Return true if is an XML HTTL request.

Test to see if a request contains the HTTP_X_REQUESTED_WITH header.

    public static function isAjaxRequest()

Return true if the request is an AJAX call.

Sample:

    $ajax = Springy\URI::isAjaxRequest();