2012-08-29 5 views
5

にコントローラとアクション名を取得するにはどのように、我々はZF2にこれを達成するにはどうすればよいZF1にZF2

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

を使用してコントローラとアクション名を取得することができますか?

UPDATE: 私は彼らが

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

を使用して取得しようとしましたが、私は、私は__construct()メソッドでそれらを取得したいエラー

Fatal error: Call to a member function getParam() on a non-object 

を取得しています。

理想的には、アクションが定義されていないかどうかを確認したいのですが、noaction()メソッドを実行します。私はphpメソッドmethod_existsを使ってチェックします。

答えて

4

あなたは、コントローラ__construct()方法でこれらの変数にアクセスすることはできませんが、dispatch方法とonDispatch方法でそれらにアクセスすることができます。

いますが、アクションが存在するかどうかをチェックしたい場合は、ZF2にすでに

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

以下のように、そのnotFoundActionのための機能に組み込まれていますが、まだ手動でそれをしたい場合は、あなたが行うことができますがありこの

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

こんにちは、私のコントローラにあなたのコードを挿入すると、ブラウザは何も表示しません(サーバとして表示されますデータを送信しない)。何が起こったのか教えていただけますか?ありがとうございました –

12

さらに簡単:

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

動作しません。同じエラー致命的なエラー:非オブジェクト上のメンバー関数getParam()を呼び出します。私は何か間違っているのですか?名前空間を使用するか、クラスを拡張する必要がありますか?私は__contruct()メソッドでこれを呼び出しています。 – Developer

+0

コントローラからこのコールを行っていますか?どのZF2バージョンを使用していますか? –

+1

beta5、実際に私はこれらの変数がコントローラの__constructメソッドでアクセス可能ではないと思います。ディスパッチおよびonDispatchメソッドではアクセス可能です。 – Developer

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

質問の著者はzf2の実装ではなく、zf1の実装を求めました –

0

に従うよう発送方法を使用してあなたは...あなたのコントローラ内のZF2に

このようなモジュール、コントローラとアクション名を取得します
$controllerClass = get_class($this); 
$moduleNamespace = substr($controllerClass, 0, strpos($controllerClass, '\\')); 
$tmp = substr($controllerClass, strrpos($controllerClass, '\\')+1); 
$controllerName = str_replace('Controller', "", $tmp); 

//set 'variable' into layout... 
$this->layout()->currentModuleName  = strtolower($moduleNamespace); 
$this->layout()->currentControllerName = strtolower($controllerName); 
$this->layout()->currentActionName  = $this->params('action');