views:

2199

answers:

3

Case: you're developing a site with Zend Framework and need relative links to the folder the webapp is deployed in. I.e. mysite.com/folder online and localhost:8080 under development.

The following works nice in controllers regardless of deployed location:

$this->_helper->redirector->gotoSimple($action, $controller, $module, $params);

And the following inside a viewscript, ie. index.phtml:

<a href="<?php echo $this->url(array('controller'=>'index', 'action' => 'index'), null, true); ?>">

But how do I get the correct basepath when linking to images or stylesheets? (in a layout.phtml file, for example):

<img src='<?php echo WHAT_TO_TYPE_HERE; ?>images/logo.png' />

and

$this->headLink()->appendStylesheet( WHAT_TO_TYPE_HERE . 'css/default.css');

WHAT_TO_TYPE_HERE should be replaced with something that gives

<img src="/folder/images/logo.png />` on mysite.com and `<img src="/images/logo.png />

on localhost

+4  A: 

You can get the base url from the Front Controller Zend_Controller_Front::getInstance()->getBaseUrl();. I wrap that in a view helper

class My_View_Helper_BaseUrl 
{   
    /**
     *  Get base url
     * 
     * @return string
     */
    public function baseUrl()
    {
     return rtrim(Zend_Controller_Front::getInstance()->getBaseUrl(),'/');
    }

}

So in the html markup you have something like <img src="<?php echo $this->baseUrl();?>/images/logo.png"/> The trailing slash is stripped out in the helper so that when the application isn't run in a sub folder (baseUrl is blank in that case) the path will still work.

Akeem
i agree with the base URL helper, but i think that having another one for images that extends this one is a much better approach
solomongaby
A: 

This is my baseUrl helper:

class Zend_View_Helper_BaseUrl extends Zend_View_Helper_Abstract {
    public function baseUrl() {
     $protocol = $_SERVER['HTTPS'] ? 'https' : 'http';
     $server = $_SERVER['HTTP_HOST'];
     $port = $_SERVER['SERVER_PORT'] != 80 ? ":{$_SERVER['SERVER_PORT']}" : '';
     $path = rtrim(dirname($_SERVER['SCRIPT_NAME']), '/') . '/';   
     return "$protocol://$server$port$path";
    }
}
use it like this: <base href="<?= $this->baseUrl() ?>" />
+1  A: 
<?php
/**
 *
 * @package   TaMeR Library
 * @copyright (C) 2010 Dennis T Kaplan
 * @license   GPL {@link http://www.gnu.org/licenses/gpl.html}
 *
 * @author       Dennis T Kaplan
 */

/** @see Zend_View_Helper_Abstract */
require_once 'Zend/View/Helper/Abstract.php';

class TaMeR_View_Helper_BaseUrl extends Zend_View_Helper_Abstract {
    /**
     * Returns site's base url, or file with base url prepended
     *
     * $file is appended to the base url for simplicity
     *
     * @param  string|null $file
     * @return string
     */

    public function baseUrl($file = NULL) {

        $baseUrl = $domain = $subdomain = $protocol = $host = $port = NULL;

        $host = array_reverse(explode('.', $_SERVER['HTTP_HOST']));
        $domain = $host[1].'.'.$host[0];
        $subdomain = (isset($host[2]) ? $host[2] : 'www');
        if(getenv("HTTPS") == 'on') {
            $protocol = 'https';
            $port     = $_SERVER['SERVER_PORT'] != 443 ? ':'.$_SERVER['SERVER_PORT'] : '';
        }else{
            $protocol = 'http';
            $port     = $_SERVER['SERVER_PORT'] != 80 ? ':'.$_SERVER['SERVER_PORT'] : '';
        }

        // Remove trailing slashes
        if(NULL !== $file) {
            $file = '/' . ltrim($file, '/\\');
        }else{
            $file = rtrim(dirname($_SERVER['SCRIPT_NAME']), '/') . '/';
        }
        $baseUrl = $protocol.'://'.$subdomain.'.'.$domain.$port.$file;
        return $baseUrl;
    }
}
TaMeR