2013-11-01 116 views
0

我試圖匹配當前URL,用戶是在一個路線,並輸出一個「積極的」類,如果在路由匹配:Zend框架2匹配當前URL路由

<?php if($this->url() == $this->url('dashboard')) echo "class='active'" ?> 

這部作品存在的路線。但是,這會弄亂404路由;如果我去了一個不存在的路徑,即本地主機/ someurl,我得到:

Fatal error: Uncaught exception 'Zend\View\Exception\RuntimeException' with message 'No RouteMatch instance provided' 

什麼是去了解這一點的最好方法是什麼?

+0

未設置404 routemath。捕捉異常怎麼樣? –

回答

0

你試試:

控制器:

public function dashboardAction(){ 
    return new ViewModel(array(
      'dashboard' => true, 
    )); 
} 

查看:

<div class="<?php echo $this->dashboard ? 'active' : ''?> other-class">Dashboard</div> 

好運。 :D

+0

這適用於儀表板視圖,但我在儀表板視圖之外呈現我的導航.. – Ethan

+0

您嘗試:$ this-> layout() - > setTemplate('tpl-path/tpl-name',array(' dashboard'=> true)); [導航](https://packages.zendframework.com/docs/latest/manual/en/modules/zend.navigation.quick-start.html) –