2012-08-29 17 views
5

yılında kontrolörü ve eylem adını almak için, biz ZF2 içinde bunu başarmak nasılnasıl zf1 yılında ZF2

$controller = $this->getRequest()->getControllerName(); 
$action = $this->getRequest()->getActionName(); 

kullanarak kontrolörü ve eylem adı alabilirim? GÜNCELLEME

: Ben

echo $this->getEvent()->getRouteMatch()->getParam('action', 'NA'); 
echo $this->getEvent()->getRouteMatch()->getParam('controller', 'NA'); 

kullanarak bunları almaya çalıştı Ama hata

Onları __construct() yöntemini olsun ister
Fatal error: Call to a member function getParam() on a non-object 

alıyorum;

İdeal olarak, Eylem belirtilmemişse, noaction() yöntemini çalıştıracak olup olmadığını kontrol etmek isterim. Php method method_exists kullanarak kontrol ederdim.

cevap

4

denetleyici __construct() yönteminde bu değişkenleri erişemez, ancak dispatch yöntemi ve onDispatch yönteminde erişebilirsiniz.

ancak eylem mevcut olup olmadığını kontrol etmek istiyorsanız, ZF2 zaten bir

public function notFoundAction() 
{ 
    parent::notFoundAction(); 
    $response = $this->getResponse(); 
    $response->setStatusCode(200); 
    $response->setContent("Action not found"); 
    return $response; 
} 

aşağıdaki gibi olduğu notFoundAction için işlevinde inşa ancak yine elle yapmak isterseniz yapabilirsiniz yoktur izleyin olarak kullanıyor sevk yöntemleri

namespace Mynamespace\Controller; 

use Zend\Mvc\Controller\AbstractActionController; 

use Zend\Stdlib\RequestInterface as Request; 
use Zend\Stdlib\ResponseInterface as Response; 
use Zend\Mvc\MvcEvent; 

class IndexController extends AbstractActionController 
{ 

    public function __construct() 
    { 


    }   

     public function notFoundAction() 
    { 
     parent::notFoundAction(); 
     $response = $this->getResponse(); 
     $response->setStatusCode(200); 
     $response->setContent("Action not found"); 
     return $response; 
    } 

    public function dispatch(Request $request, Response $response = null) 
    { 
     /* 
     * any customize code here 
     */ 

     return parent::dispatch($request, $response); 
    } 
    public function onDispatch(MvcEvent $e) 
    { 
     $action = $this->params('action'); 
     //alertnatively 
     //$routeMatch = $e->getRouteMatch(); 
     //$action = $routeMatch->getParam('action', 'not-found'); 

     if(!method_exists(__Class__, $action."Action")){ 
      $this->noaction(); 
     } 

     return parent::onDispatch($e); 
    } 
    public function noaction() 
    {   
     echo 'action does not exits'; 
    } 
} 
+0

Merhaba, kodunuzu denetleyicime yerleştirdiğimde, tarayıcı hiçbir şey göstermiyor (sunucu olarak) veri göndermeme). Bana ne olduğunu anlatabilir misin? Teşekkürler –

12

Hatta daha basit:

$controllerName =$this->params('controller'); 
$actionName = $this->params('action'); 
+0

çalışmıyor. aynı hata Önemli hata: Nesne dışı bir getParam() işlevinin çağrılması. Yanlış bir şey mi yapıyorum?? Herhangi bir isim alanını kullanmam veya herhangi bir dersi uzatmam gerekir mi? Bunu __contruct() yönteminde arıyorum. – Developer

+0

Bu çağrıyı bir denetleyiciden mi yapıyorsunuz? Hangi ZF2 sürümünü kullanıyorsunuz? –

+1

beta5, aslında bu değişkenlerin denetleyicinin __construct yönteminde erişilemez olduğunu düşünüyorum. ancak gönderim ve onDispatch yöntemlerinde akredite olabilir. – Developer

-6
$controllerName = strtolower(Zend_Controller_Front::getInstance()->getRequest()->getControllerName()); 
+5

zf1 değil, zf1 uygulama için sorulan soru yazarı –

0

Sen ... controller içerisinde ZF2 böyle modül, kontrolör ve eylem adını alacak