Pergunta

Estou criando um ajudante de ação que exigirá o valor de retorno do

Zend_View_Helper_BaseUrl

Como faço para fazer isso?

Foi útil?

Solução

$this->view->baseUrl() Deveria trabalhar.

Mas sugiro criar um novo ajudante de ação, que é basicamente uma cópia do Helper, mas você pode modificar para atender às suas necessidades:

/**
 * 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;
    }
}

Outras dicas

Você pode obter uma alça para a vista de qualquer lugar do aplicativo com:

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

Há uma chance de que a visão ainda não seja inicializada, mas de um thelper de ação que não deve ser um problema. Você também pode usar o URL usado pelo ajudante BaseUrl View com:

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

Não posso verificar agora, mas acredito que um ajudante de ação terá acesso ao controlador via $this->getActionController() que tem um public $view assim:

 $baseUrl = $this->getActionController()->view->baseUrl();
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top