5

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/');
}

4 Answers 4

6

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

Sign up to request clarification or add additional context in comments.

Comments

3

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();

2 Comments

What is the class to be included for using the getHTTPPost() ?
In controller : Call to undefined method Zend\Http\PhpEnvironment\Request::getHttpHost()
1
protected function _getPublicPath() {
    chdir(APPLICATION_PATH);
    return realpath("../public");
}

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.