3

の戻り値を必要とするアクションヘルパーを作成しています

Zend_View_Helper_BaseUrl

どうすればいいですか?

4

3 に答える 3

5

$this->view->baseUrl()動作するはずです。

ただし、基本的にビューヘルパーのコピーである新しいアクションヘルパーを作成することをお勧めしますが、ニーズに合わせて変更することもできます。

/**
 * 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;
    }
}
于 2010-04-19T10:14:07.000 に答える
3

次のようにして、アプリのどこからでもビューへのハンドルを取得できます。

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

ビューがまだ初期化されていない可能性がありますが、問題にはならない ActionHelper からのものです。BaseUrl ビュー ヘルパーで使用される URL を取得することもできます。

Zend_Controller_Front::getInstance()->getBaseUrl();
于 2010-04-19T08:11:04.340 に答える
2

現時点では確認できませんが、アクション ヘルパーは次$this->getActionController()のようにしてコントローラーにアクセスできると思いますpublic $view

 $baseUrl = $this->getActionController()->view->baseUrl();
于 2010-04-19T07:18:49.263 に答える