2013-04-25 28 views
0

我正在使用以下代碼來使用路由,但是我將爲不同的路由重複相同的代碼。Slim Framework:D.R.Y

問題:避免重複的最佳方法是什麼?我嘗試過使用方法getParameters,但這不起作用,因爲當我返回參數時,我需要將它們設置爲一個變量,這會使代碼變得冗餘。或者,也許我看錯了方式。

我想使用「幹」(不要重複自己)。有人可能會更改參數名稱,所以這很有幫助。

$app->get('/blog', function() use ($app){ //same code as below }); 


$app->get('/link', function() use ($app){ 
$link = new linksApi(); 

//call question api 


$username = $app->request()->params('username'); 
$company = $app->request()->params('company'); // tags 
$follower = $app->request()->params('follower');  
$max = $app->request()->params('max');  
$date = $app->request()->params('date');  
$date_value = $app->request()->params('date_value');  
$oldest = $app->request()->params('oldest');  
$counts = $app->request()->params('counts');  
$sorts = $app->request()->params('sorts');  
$counts = $app->request()->params('counts');  
$format = $app->request()->params('format');  

}); 

回答

0

不明智的使用它,如果你擔心性能,但這裏有雲:

$routes = array(
    'username', 
    'company', 
    'follower', 
    'max' 
    // ... 
); 

foreach($routes as $r) { 
    ${$r} = $app->request()->params($r); 
}