Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice to create absolute URLs with Zend framework?

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.

like image 514
ck. Avatar asked Dec 10 '09 14:12

ck.


People also ask

What's the difference between an absolute URL and a relative URL?

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.

How do you create a project in Zend Framework?

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.

When to use a relative URL?

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.

Is Zend framework in PHP?

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.


3 Answers

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
like image 172
Tomáš Fejfar Avatar answered Oct 18 '22 16:10

Tomáš Fejfar


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);
like image 38
Mr Coder Avatar answered Oct 18 '22 16:10

Mr Coder


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.

like image 2
Mohammad Emran Hasan Avatar answered Oct 18 '22 18:10

Mohammad Emran Hasan