开发者

zendframework baseUrl view helper in bootstrap

My website uses the zend framework and runs in a sub folder, for example: http://www.example.com/sub/folder. Now I want to prepend my css links with /sub/folder/ to make the css load in pages like http://www.example.com/sub/folder/product/abc, I thought I found a view helper to do this BaseUrl but BaseUrl seems only to work in actual view files and not in a bootstrap class. Does anybody know the reason for this and an exceptable workaround?

This is Snippet of my boodstrap class.

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initStylesheet()
    {
        $this->_logger->info('Bootstrap ' . __METHOD__);

        $this->bootstrap('view');
        $this->_view = $this->getResource('view');

        $this->_view->headLink()->appendStylesheet($this->_view->baseUrl('/cs开发者_如何学Gos/main.css'));
    }
}


I found the reason why the baseUrl() view helper didn't work in the Bootstrap. It's because Zend_Controller_Front didn't have a Zend_Controller_Request.

So to solve the issue I added an other resource method to initialize the request.

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initRequest()
    {
        $this->_logger->info('Bootstrap ' . __METHOD__);

        $this->bootstrap('FrontController');

        $front = $this->getResource('FrontController');
        $request = new Zend_Controller_Request_Http();

        $front->setRequest($request);
    }

    protected function _initStylesheet()
    {
        $this->_logger->info('Bootstrap ' . __METHOD__);

        $this->bootstrap('view');
        $this->_view = $this->getResource('view');

        $this->_view->headLink()->appendStylesheet($this->_view->baseUrl('/css/main.css'));
    }
}


To get the baseUrl you could use

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

or a non ZF solution:

substr($_SERVER['PHP_SELF'], 0, -9);


Actually, you don't need get the baseUrl 'cause ZF already does it for you. You just have to pay attention to your path. Do not use the first slash! otherwise ZF will return the remote address.

Just use '$this->_view->headLink()->appendStylesheet('css/main.css');'

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜