Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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/');
}
like image 597
Somal Somalski Avatar asked Feb 22 '12 01:02

Somal Somalski


1 Answers

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! :-)

like image 98
JamesG Avatar answered Nov 12 '22 19:11

JamesG