bootstrap içinde zendframework baseURL görünüm yardımcı

3 Cevap php

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?

Bu benim boodstrap sınıf Snippet.

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('/css/main.css'));
    }
}

3 Cevap

Ben baseUrl() görünümü yardımcı Bootstrap işe yaramadı neden nedeni bulundu. Zend_Controller_Front bir Zend_Controller_Request yoktu çünkü.

Yani isteği başlatmak için bir diğer kaynak yöntemi eklendi sorunu çözmek için.

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'));
    }
}

BaseURL almak için kullanabilirsiniz

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

ya da olmayan bir ZF çözeltisi:

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

Aslında, ZF zaten sizin için yapar baseURL 'neden almak gerekmez. Sadece senin yoluna dikkat etmeniz gerekiyor. Ilk çizgi kullanmayın! Aksi ZF uzak adresi dönecektir.

Sadece '$this->_view->headLink()->appendStylesheet('css/main.css'); 'kullanın