Getting the baseurl value into my controllers

17,583

Solution 1

$this->view->baseUrl() should work.

But I suggest creating new action helper, which is basically a copy of view helper, but you may modify to suit your needs:

/**
 * Generate URL of the current domain
 *
 */
class My_Controller_Action_Helper_BaseUrl
extends Zend_Controller_Action_Helper_Abstract
{
    public function direct($file = null, $full = true)
    {
        return $this->baseUrl($file, $full);
    }

    /**
     * BaseUrl
     *
     * @var string
     */
    protected $_baseUrl;

    /**
     * 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)
    {
        // Get baseUrl
        $baseUrl = $this->getBaseUrl();

        // Remove trailing slashes
        if (null !== $file) {
            $file = '/' . ltrim($file, '/\\');
        }

        return $baseUrl . $file;
    }

    /**
     * Set BaseUrl
     *
     * @param  string $base
     * @return My_Controller_Action_Helper_BaseUrl
     */
    public function setBaseUrl($base)
    {
        $this->_baseUrl = rtrim($base, '/\\');
        return $this;
    }

    /**
     * Get BaseUrl
     * @return string
     */
    public function getBaseUrl()
    {
        if ($this->_baseUrl === null) {
            /** @see Zend_Controller_Front */
            require_once 'Zend/Controller/Front.php';
            $baseUrl = Zend_Controller_Front::getInstance()->getBaseUrl();

            // Remove scriptname, eg. index.php from baseUrl
            $baseUrl = $this->_removeScriptName($baseUrl);

            $this->setBaseUrl($baseUrl);
        }

        return $this->_baseUrl;
    }

    /**
     * Remove Script filename from baseurl
     *
     * @param  string $url
     * @return string
     */
    protected function _removeScriptName($url)
    {
        if (!isset($_SERVER['SCRIPT_NAME'])) {
            // We can't do much now can we? (Well, we could parse out by ".")
            return $url;
        }

        if (($pos = strripos($url, basename($_SERVER['SCRIPT_NAME']))) !== false) {
            $url = substr($url, 0, $pos);
        }

        return $url;
    }
}

Solution 2

You can get a handle to the view from anywhere in you app with:

$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
$view = $viewRenderer->view;

There is a chance that the view won't be initialized yet but from an ActionHelper that shouldn't be a problem. You could also get the URL used by the BaseUrl view helper with:

Zend_Controller_Front::getInstance()->getBaseUrl();

Solution 3

I'm not able to verify right now, but I believe that an Action Helper is going to have access to the controller via $this->getActionController() which has a public $view so:

 $baseUrl = $this->getActionController()->view->baseUrl();
Share:
17,583

Related videos on Youtube

davykiash
Author by

davykiash

Software Engineer | Technologist | Futurist

Updated on June 04, 2022

Comments

  • davykiash
    davykiash almost 2 years

    Am creating an action helper that will require the return value of the

    Zend_View_Helper_BaseUrl
    

    How do I go about that?