Oto Shavadze
Oto Shavadze

Reputation: 42853

How is defined current url in zend (inside job of a framework)

Tell please what script uses zend framework for definition current URL? More exactly I interest what use ZEND for definition domain name: this $_SERVER['HTTP_HOST'] or this $_SERVER['SERVER_NAME'] ? (or may be something other)?

P.S. ( I search in documentation but not found, (I do not know this framework), also I search in google, but also not found answer on my question? )

Upvotes: 3

Views: 7000

Answers (1)

Marin Sagovac
Marin Sagovac

Reputation: 3992

Try use: $this->getRequest()->getRequestUri() to get current of requested URI.

In the view script use: $this->url() to get current URL.

Or using via static integrated Zend Controller front via instance:

$uri = Zend_Controller_Front::getInstance()->getRequest()->getRequestUri();

You can get a value of URI implementation via singleton to get a value of request() data:

$request = Zend_Controller_Front::getInstance()->getRequest();
$url = $request->getScheme() . '://' . $request->getHttpHost();

On the View use it as:

echo $this->serverUrl(true); # return with controller, action,...

You should avoid hardcode such as example (NOT TO USE!):

echo 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'];

instead of this example use as on a view:

$uri = $this->getRequest()->getHttpHost() . $this->view->url();

If you want using getRequest in ZEND more explore The Request Object.

SKIP IT BELOW (AUTOPSY EXAMPLE HOW WORKS IT).

Full of example code how getRequestUri() how it works and why is isRequest instead using $_SERVER is because on a platform specific is randomly get a data:

first if uri null, thand if requested from IIS set is as HTTP_X_REWRITE_URL. If not, check on IIS7 rewritten uri (include encoded uri). If not on IIS than REQUEST_URI will check scheme of HTTP_HOSTNAME, or if failed use as ORIG_PATH_INFO and grab a QUERY_STRING.

If is setted, grab a data automatically via string of returned object $this in a class.

If failed, than will be set a parsed string than set it.

if ($requestUri === null) {
        if (isset($_SERVER['HTTP_X_REWRITE_URL'])) { // check this first so IIS will catch
            $requestUri = $_SERVER['HTTP_X_REWRITE_URL'];
        } elseif (
            // IIS7 with URL Rewrite: make sure we get the unencoded url (double slash problem)
            isset($_SERVER['IIS_WasUrlRewritten'])
            && $_SERVER['IIS_WasUrlRewritten'] == '1'
            && isset($_SERVER['UNENCODED_URL'])
            && $_SERVER['UNENCODED_URL'] != ''
            ) {
            $requestUri = $_SERVER['UNENCODED_URL'];
        } elseif (isset($_SERVER['REQUEST_URI'])) {
            $requestUri = $_SERVER['REQUEST_URI'];
            // Http proxy reqs setup request uri with scheme and host [and port] + the url path, only use url path
            $schemeAndHttpHost = $this->getScheme() . '://' . $this->getHttpHost();
            if (strpos($requestUri, $schemeAndHttpHost) === 0) {
                $requestUri = substr($requestUri, strlen($schemeAndHttpHost));
            }
        } elseif (isset($_SERVER['ORIG_PATH_INFO'])) { // IIS 5.0, PHP as CGI
            $requestUri = $_SERVER['ORIG_PATH_INFO'];
            if (!empty($_SERVER['QUERY_STRING'])) {
                $requestUri .= '?' . $_SERVER['QUERY_STRING'];
            }
        } else {
            return $this;
        }
    } elseif (!is_string($requestUri)) {
        return $this;
    } else {
        // Set GET items, if available
        if (false !== ($pos = strpos($requestUri, '?'))) {
            // Get key => value pairs and set $_GET
            $query = substr($requestUri, $pos + 1);
            parse_str($query, $vars);
            $this->setQuery($vars);
        }
    }

    $this->_requestUri = $requestUri;
    return $this;

Upvotes: 6

Related Questions