Is there a best practice for creating absolute URLs using the Zend framework? I wonder if there is some helper or if this would just be concatenating the scheme, host, etc. from the $_SERVER variable and then add the relative path generated by Zend.
An absolute URL contains all the information necessary to locate a resource. A relative URL locates a resource using an absolute URL as a starting point.
Open a terminal (in Windows, Start -> Run, and then use cmd). Navigate to a directory where you would like to start a project. Then, use the path to the appropriate script, and execute one of the following: % zf create project quickstart.
A relative URL is useful within a site to transfer a user from point to point within the same domain. Absolute links are good when you want to send the user to a page that is outside of your server.
Zend is an open source PHP framework. It is pure object-oriented and built around the MVC design pattern. Zend framework contains collection of PHP packages which can be used to develop web applications and services. Zend was started by Andi Gutmans and Zeev Suraski.
phpfour's way is OK, but you have to check for https://, ftp:// and mailto: too... :)
I prefefer having all urls root-absolute (/files/js/jquery.js
). The "hardcore zend way" is
<?php
// document root for example.com is in /htdocs
// but application's index.php resides in /htdocs/myapp/public
echo $this->baseUrl('css/base.css');
//will return /myapp/public/css/base.css
echo $this->serverUrl() . $this->baseUrl('css/base.css');
//will return http://www.example.com/myapp/public/css/base.css
echo '//' . $this->getHelper('ServerUrl')->getHost() . $this->baseUrl('css/base.css');
//will return protocol relative URL //www.example.com/myapp/public/css/base.css
Without mvc
echo $this->serverUrl() . $this->baseUrl('cass/base.css');
or with mvc
echo $this->serverUrl() . $this->url(array('controller'=>'index','action'=>'index'),null,true);
In my applications, I keep a "baseUrl" in my application config and I assign that to registry on bootstrapping. Later I use the following View Helper to generate the URL:
<?php
class Zend_View_Helper_UrlMap
{
public function UrlMap($original)
{
$newUrl = $original;
$baseUrl = Zend_Registry::get('baseUrl');
if (strpos($newUrl, "http://") === false) {
$newUrl = $baseUrl . $newUrl;
}
return $newUrl;
}
}
Benefit: I can make any change on all the URLs in the view from one place.
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With