Getting baseurl value in my controllers

Create an action helper that needs a return value

Zend_View_Helper_BaseUrl

How should I do it?

+3
source share
3 answers

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

But I suggest creating a new action helper, which is basically a copy of the view helper, but you can change it 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 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;
    }
}
+4
source

You can get the view descriptor from anywhere in the application:

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

It is likely that the view will not be initialized yet, but from an ActionHelper, which should not be a problem. You can also get the URL used by the BaseUrl view assistant using:

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

I can’t check right now, but I believe that the Action Helper will access the controller through $this->getActionController(), which has public $viewthis:

 $baseUrl = $this->getActionController()->view->baseUrl();
+2
source

Source: https://habr.com/ru/post/1741734/


All Articles