2009-03-11 1 views
11

사례 : Zend Framework를 사용하여 사이트를 개발 중이며 webapp가 배포 된 폴더에 대한 상대 링크가 필요합니다. mysite.com/folder 온라인 및 localhost:8080 개발 중입니다.젠드 프레임 워크의 뷰에서 기본 경로 가져 오기

$this->_helper->redirector->gotoSimple($action, $controller, $module, $params); 

을 그리고 즉 viewscript, 내부에 다음

다음에 관계없이 배치 위치의 컨트롤러에 좋은 작동합니다. index.phtml :

<a href="<?php echo $this->url(array('controller'=>'index', 'action' => 'index'), null, true); ?>"> 

그러나 이미지 나 스타일 시트에 연결할 때 올바른 기본 경로를 얻으려면 어떻게해야합니까? (예를 들어 layout.phtml 파일에) :

<img src='<?php echo WHAT_TO_TYPE_HERE; ?>images/logo.png' /> 

$this->headLink()->appendStylesheet(WHAT_TO_TYPE_HERE . 'css/default.css'); 

WHAT_TO_TYPE_HERE

는 로컬 호스트

답변

16

당신은 수에

<img src="/folder/images/logo.png />` on mysite.com and `<img src="/images/logo.png /> 

을 제공 무언가로 대체해야 전면 컨트롤러 Zend_Controller_Front::getInstance()->getBaseUrl();에서 기본 URL을 가져옵니다. (base을가 비어 응용 프로그램이 하위 폴더에서 실행되지 않을 때 그래서 나는보기 도우미는 <img src="<?php echo $this->baseUrl();?>/images/logo.png"/> 같은이 후행 슬래시가 도우미에서 제거되고있는 HTML 마크 업에 따라서

class My_View_Helper_BaseUrl 
{ 
    /** 
    * Get base url 
    * 
    * @return string 
    */ 
    public function baseUrl() 
    { 
     return rtrim(Zend_Controller_Front::getInstance()->getBaseUrl(),'/'); 
    } 

} 

에 그 포장 이 경우 경로는 계속 작동합니다.

+0

기본 URL 도우미에 동의하지만이 이미지를 확장하는 다른 이미지를 사용하는 것이 훨씬 더 나은 접근 방법이라고 생각하십시오. –

4

이 내 base을 도우미 : 사람이 가장 좋은 방법을 알고 싶어하고 젠드/구글 검색 2 시간 낭비하고 싶지 않습니다

class Zend_View_Helper_BaseUrl extends Zend_View_Helper_Abstract { 
    public function baseUrl() { 
     $protocol = isset($_SERVER['HTTPS']) ? 'https' : 'http'; 
     $server = $_SERVER['HTTP_HOST']; 
     $port = $_SERVER['SERVER_PORT'] != 80 ? ":{$_SERVER['SERVER_PORT']}" : ''; 
     $path = rtrim(dirname($_SERVER['SCRIPT_NAME']), '/\\') . '/'; 
     return "$protocol://$server$port$path"; 
    } 
} 
+0

0
<?php 
/** 
* 
* @package TaMeR Library 
* @copyright (C) 2010 Dennis T Kaplan 
* @license GPL {@link http://www.gnu.org/licenses/gpl.html} 
* 
* @author  Dennis T Kaplan 
*/ 

/** @see Zend_View_Helper_Abstract */ 
require_once 'Zend/View/Helper/Abstract.php'; 

class TaMeR_View_Helper_BaseUrl extends Zend_View_Helper_Abstract { 
    /** 
    * 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) { 

     $baseUrl = $domain = $subdomain = $protocol = $host = $port = NULL; 

     $host = array_reverse(explode('.', $_SERVER['HTTP_HOST'])); 
     $domain = $host[1].'.'.$host[0]; 
     $subdomain = (isset($host[2]) ? $host[2] : 'www'); 
     if(getenv("HTTPS") == 'on') { 
      $protocol = 'https'; 
      $port  = $_SERVER['SERVER_PORT'] != 443 ? ':'.$_SERVER['SERVER_PORT'] : ''; 
     }else{ 
      $protocol = 'http'; 
      $port  = $_SERVER['SERVER_PORT'] != 80 ? ':'.$_SERVER['SERVER_PORT'] : ''; 
     } 

     // Remove trailing slashes 
     if(NULL !== $file) { 
      $file = '/' . ltrim($file, '/\\'); 
     }else{ 
      $file = rtrim(dirname($_SERVER['SCRIPT_NAME']), '/') . '/'; 
     } 
     $baseUrl = $protocol.'://'.$subdomain.'.'.$domain.$port.$file; 
     return $baseUrl; 
    } 
} 
+0

사용 가능한 코드를 다시 작성 했습니까 ?? – Alexar

14

경우. 이를 수행하는보기 도우미가 있습니다.

$this->view->serverUrl(); 
+1

수정 : $ this-> serverUrl(); – mzalazar

5

그렇게이를 인쇄하여 HOST 이름을 얻을 당신의 layout 파일에 이름을 호스트 할 할 경우 (

는 $ this- 에코> SERVERURL :

echo $this->serverUrl().$this->baseUrl()