Somal Somalski
Somal Somalski

Reputation: 578

Zend Framework: get public path, get application url

Are there some proper Zend methods for:

a) receiving path to /public directory

b) receiving application url

Actually I'm using methods defined in Controller, but it feel right to use ready methods if they exits.

protected function _getApplicationUrl() {
    return $_SERVER['SERVER_NAME'];
}

protected function _getPublicPath() {
    return realpath(APPLICATION_PATH . '/../public/');
}

Upvotes: 5

Views: 19422

Answers (4)

Vasiliy Toporov
Vasiliy Toporov

Reputation: 875

a) receiving path to /public directory

Built-in php-function getcwd() will give you the path to your site-host folder (ex. output "/home/my_cp/public_html/my_site.loc/www"). And then, you can construct whatever path you want.

Upvotes: 1

ecdeveloper
ecdeveloper

Reputation: 2875

protected function _getPublicPath() {
    chdir(APPLICATION_PATH);
    return realpath("../public");
}

Upvotes: 1

Igor Vizma
Igor Vizma

Reputation: 370

1) If your virtual host point to ZF /public then in View you can get path by helper method $this->baseUrl(); In controller $this->view->baseUrl(); Otherwise create your own helper and use it. 2) In controller $this->getRequest()->getHttpHost();

Upvotes: 3

JamesG
JamesG

Reputation: 4309

Regarding the application URL, Zend_Controller_Request_Http has a getRequestUri() method, but it deliberately (and somewhat frustratingly) excludes the scheme and hostname parts of the URL. In my apps I have resorted to grabbing $_SERVER['HTTP_HOST'] in the bootstrap and storing it in the registry so that I can use it later when constructing full URLs.

And from memory, no, there isn't any built-in method to get the location of the public folder, but the code you have is fine. Most apps I've seen define() all the paths in index.php, which I suppose is slightly safer (only because the path names get set sooner and become absolutely immutable) and ever so slightly faster, but lets not get into a debate about micro-optimizations! :-)

Upvotes: 6

Related Questions