2013-04-15 57 views
0

爲什麼params插件魔術功能默認轉發路由?Params插件魔術方法功能

爲什麼zf無法從類似下面的示例中獲取函數?

public function __invoke($param = null,$type='Route', $default = null) 
{ 
    if ($param === null) { 
     return $this; 
    } 
    $type = 'from'.ucfirst($type); 
    // Need to check function exist, if not must throw some error 
    return $this->$type($param, $default); 
} 

示例使用

$this->params('name','post'); 
$this->params('token','get'); 
$this->params('action'); // this will return from route like default one. 

我怎麼能延長默認PARAMS插件這樣嗎?這是一個很好的舉措?

回答

0

默認的參數總是從Route中獲取。這是因爲ZF2鼓勵人們做大量的手動路由。這既是速度目的,也是SEO目的。

$this->params('paramname', 'defaultValueIfNotFound'); 

在你需要從請求的特定區域的PARAMS的情況下,你能做到這一點,也使用PARAMS-插件本身。但是,這已經大大@Matsemann

你可以擴展PARAMS插件,但你應該使用第三個參數的附加選項。但請記住,另一種方法更簡潔,並且最終需要較少的插件工作。沒有switch -statement只是爲了得到一個參數;)

+0

謝謝薩姆。我需要了解更多zf2的方式解決:) 我正在閱讀您的博客文章,我發現這個https://github.com/zendframework/zf-web項目,我認爲它會幫助我瞭解更多。 –