问题
I understand that baseUrls are mostly automatically set in zend projects, and they come in handy when running an app from a subfolder, But I'm running my app from the root public folder, with server-name myzend.loc.
So when I do an echo $this->baseUrl() or var_dump($this->baseUrl()), I get an empty string ''. Is this normal and a result of not running the app from a subfolder? or do I set baseUrl manually; eg $request->getHelper('baseUrl')->setBaseUrl($_SERVER['SERVER_NAME']);
回答1:
The quick answer is that on my ZF1 apps serving sites at the root of a virtual web host, I also get an empty string for:
Zend_Controller_Front::getInstance()->getBaseUrl()
As @TimFountain notes, on its own, that doesn't return a true URL, complete with http or https protocol prefix. So, it can be interpreted more as a file-path than as a URL. But it's more of a relative file path: the path relative to the docroot.
And, of course, if you set the baseUrl - perhaps as a real URL complete with protocol prefix - then all interpretations are off: it merely returns whatever you set.
It is also worth noting two things about the baseUrl() view-helper implemented in Zend_View_Helper_BaseUrl:
It uses the front-controller
getBaseUrl()method above.It takes an argument!
For (2), in my experience, many people use the baseUrl() view-helper as follows (in a view-script, say):
<?php echo $this->baseUrl() . '/assets/css/styles.css'; ?>
But I find it more concise to write:
<?php echo $this->baseUrl('/assets/css/styles.css'); ?>
To me, it's not just a matter of a few extra characters. It's more that the core of the app should know his own internal structure, but not his deployment details. Employing the baseUrl() view-helper this way implicitly acknowledges that there some deployment-aware mapping required to resolve an internal deployment-independent asset reference to its post-deployment counterpart.
回答2:
$this->baseUrl() is setted by the zend framework..you dont need to set..the reason why you are getting an empty baseurl is in your apache httpd.conf your Virtualhost documentroot is setted up to the project directory
DocumentRoot "/usr/local/apache2/docs/yourproject"
回答3:
Despite the name, baseUrl returns an absolute path, not a URL. It returning an empty string would be what you'd expect from a normal ZF project that doesn't live in a sub-folder.
来源:https://stackoverflow.com/questions/15132691/do-i-set-my-zend-baseurl-manually