Getting basepath from view in zend framework
Asked Answered
M

6

11

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

Muna answered 11/3, 2009 at 18:12 Comment(0)
S
16

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.

Sola answered 11/3, 2009 at 18:26 Comment(1)
i agree with the base URL helper, but i think that having another one for images that extends this one is a much better approachHawaiian
M
14

In case anyone wants to know the best way and doesn't want to waste 2 hours searching Zend/Google. There is a view helper to do that.

$this->view->serverUrl();
Myth answered 29/9, 2011 at 19:31 Comment(1)
Correction: $this->serverUrl();Stinking
W
5

If do you want to host name in your layout file so print this and get your HOST name:

echo $this->serverUrl().$this->baseUrl()
Wives answered 17/2, 2012 at 8:43 Comment(0)
P
4

This is my baseUrl helper:

class Zend_View_Helper_BaseUrl extends Zend_View_Helper_Abstract {
    public function baseUrl() {
        $protocol = isset($_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";
    }
}
Pentane answered 4/7, 2009 at 12:40 Comment(1)
use it like this: <base href="<?= $this->baseUrl() ?>" />Pentane
D
0
<?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;
    }
}
Deuterogamy answered 19/3, 2010 at 18:50 Comment(0)
H
0

This worked for me:

echo $this->serverUrl() . $this->url()

Hilel answered 5/1, 2013 at 3:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.