【问题标题】:Slim Framework: D.R.Y超薄框架:D.R.Y
【发布时间】:2013-04-25 01:09:58
【问题描述】:

我正在使用以下代码来使用路线,但我会为不同的路线重复相同的代码。

问题:避免重复的最佳方法是什么?我尝试过使用 getParameters 方法,但这不起作用,因为当我返回参数时,我需要将它们设置为变量,这使得代码变得多余。或者也许我看错了。

我正在尝试使用“DRY”(不要重复自己)。有人可能会更改参数名称,所以这很有帮助。

$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');    

});

【问题讨论】:

    标签: php frameworks slim


    【解决方案1】:

    如果您担心性能,则使用它是不明智的,但它就是这样:

    $routes = array(
        'username',
        'company',
        'follower',
        'max'
        // ...
    );
    
    foreach($routes as $r) {
        ${$r} = $app->request()->params($r);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-06
      • 2018-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-11
      • 1970-01-01
      相关资源
      最近更新 更多