2011-03-18 3 views
3

내 문제의 해결책을 어디에서 찾을 수 없으며 SO 또는 실제 젠드 문서에서 찾을 수 없습니다.Zend Framework가 플러그인의 뷰 위치를 변경합니다.

  1. 나는 사용자가 현재 모바일을 사용하는 경우 감지 Zend_Http_UserAgent 및 WURFL를 사용하여 플러그인을 만들었습니다

    기본적으로 내가이 설정을 가지고있다. 이것은 사전 배포에서 수행됩니다. 이게 잘 작동합니다.

  2. 이제 사용자가 모바일을 사용하고 있다면 뷰 스크립트 디렉토리를 변경하고 싶습니다.

  3. 완벽한 세계에서 이상적으로보기의 모바일 버전을로드하고 싶습니다. 존재한다면, 기본보기를로드하십시오. 그러나 나는 적어도 2 일할 수 있다면 나는 행복 할거야.

나는 어떻게 이런 일을 저지른 것일까 요. 나는 내가 사용할 수있는 것을 보았다 :

$ view = Zend_Controller_Action_HelperBroker :: getStaticHelper ('viewRenderer') -> view; $ view-> setBasePath (APPLICATION_PATH. '/ mobile_views /');

하지만 그게 나던 일을하는 것 같아요, 이런 일이 preDispatch에서 일어나야한다고 생각할 때 postDispatch에서 발생합니다. 여기

내 현재 플러그인입니다 :

<?php 

클래스 SQ_Plugins_Mobile이 Zend_Controller_Plugin_Abstract {

public function dispatchLoopStartup(Zend_Controller_Request_Abstract $flag) { 

    $bootstrap = Zend_Controller_Front::getInstance()->getParam("bootstrap"); 
    $useragent = $bootstrap->getResource("useragent"); 
    $device  = $useragent->getDevice(); 

    Zend_Registry::set("useragent", $useragent); 
    Zend_Registry::set("device", $device); 

    echo $device->getType() . " is the type of device"; 

    if($device->getType() != "mobile") { 
     /** 
     * Set the layout to be mobile, here we make sure we streamline what is loaded 
     * so as to not load things that arent needed. 
     */ 
     Zend_Layout::getMvcInstance()->setLayout("mobile"); 


     /** 
     * Here i wish to change the view path to be APPLICATION_PATH . "/mobile_views/" 
     * @todo Change the view script path 
     */ 

    } 
} 

public function postDispatch(Zend_Controller_Request_Abstract $request) { 

    /** 
    * Maybe i have to change the view script path here? 
    * @todo change the viewscript path if we're using a mobile 
    */ 

    // Get the current view 
    $view = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->view; 
    $view->setBasePath(APPLICATION_PATH . '/mobile_views/'); 
    echo "kl;kl;kjhkl;jhlkjhjklhkl;h k;hi"; 
} 

}

+0

맞습니다.보기 변경은 preDispatch()에 설정해야합니다. –

+0

어쨌든 레지스트리에 useragent와 device를 저장하려고한다면 아마도 Bootstrap에서 다시 할 것입니다. –

+0

제공하는 콘텐츠가 사용자 에이전트에 종속되어있는 경우 해당 에이전트에 따라 다른 클라이언트에게 다른 콘텐츠를 제공하는 동일한 URL을 갖게됩니다. 아마도 모바일 버전에 대한 하위 도메인을 만들 것입니다. 하위 도메인을 웹 공간과 동일한 앱 공간으로 지정합니다. 플러그인에서 에이전트 탐지를 수행하고 사용자 에이전트가 자격이있을 때 모바일 하위 도메인으로 리디렉션합니다. chain 하위 도메인에 대한 요청에 플래그를 추가하는 호스트 이름 라우트. 그런 다음 레이아웃 /보기/접미사를 설정하기 위해 다른 플러그인에서 해당 플래그를 확인하십시오. 그냥 큰소리로 생각해. –

답변

0

OK를 확장, 그래서 내가 그건 잘 모르겠어요하지만 내가 그것을 고정했습니다 가장 좋은 '젠드 (zend)'솔루션이지만, 꽤 우아하고 작동한다고 생각합니다. 이것이 가장 좋습니다.

이제 모바일 버전을 사용하려면 모바일 버전의 동작보기를 사용할 수 있는지 확인해야합니다. 따라서 오류가 발생할 가능성이 줄어들 때 모바일 뷰를로드 할 때만 모바일 뷰를로드 할 수 있습니다.

class SQ_Plugins_Mobile extends Zend_Controller_Plugin_Abstract { 

public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request) { 

    $bootstrap = Zend_Controller_Front::getInstance()->getParam("bootstrap"); 
    $useragent = $bootstrap->getResource("useragent"); 
    $device  = $useragent->getDevice(); 

    Zend_Registry::set("useragent", $useragent); 
    Zend_Registry::set("device", $device); 

    /** 
    * @todo change this to be Mobile 
    */ 
    if($device->getType() != "mobile") { 

     /** 
     * Set the layout to be mobile, here we make sure we streamline what is loaded 
     * so as to not load things that arent needed. 
     */ 
     Zend_Layout::getMvcInstance()->setLayout("mobile_layout"); 

     /** 
     * Here we check to see if a mobile version of the template exists. if it does then we change the view suffix 
     * this allows us to load the mobile view if it exists and the defgault view if it doesnt. 
     */ 
     $base  = APPLICATION_PATH . "/views/scripts/"; 
     $mobile  = $base . $request->getControllerName() . "/" . $request->getActionName() . ".mobile.phtml"; 

     if(is_readable($mobile)) { 
      Zend_Controller_Action_HelperBroker::getExistingHelper('ViewRenderer')->setViewSuffix('mobile.phtml');  
     } 

    } 
} 
} 
+0

위의 myexample에서 볼 때마다보기가 매번 모바일보기로 강제 설정됩니다. $ device-> getType() == "mobile"은 로직이 있어야하는 것입니다. – Andy

1

모바일 버전의 페이지 인 경우이를 수행 할 수 있습니다.

$this->view->addBasePath('../application/views/mobile'); // Or whatever your path may be. 

그냥 생각해보십시오.

관련 문제