通过自定义函数访问路由变量


SlimFramework - Access Route Variables Via Custom Function

我要设置以下路由。

$app->post(
    '/:device/admin/(/)',
    'get_user',
    $validate_user(
        array(
            'Admin',
            'Manager'
        ),'
        templates'
    ),'
    render_bronzeservice'
)->conditions(
    array('device'=>'(tablet|desktop|mobile)')
);

我希望能够将路由变量:device传递给函数$validate_user

我该怎么做?

对于函数render_bronzeservice,它只是期望设备的参数,但这不适用于自定义函数,否则我无法弄清楚。

任何帮助都是感激的!

好吧,我现在找到了一个解决方案,它很丑,但它会工作。因为我在它之前的函数中获得了用户数据,所以我在那里对参数进行检查。

奇怪的是,由于某些原因,它没有将其作为字符串发送?但是将它作为路由对象发送给第一个函数。

我的get_user函数是这样的

function get_user($route){
    global $user;
    global $device;
    //If there is any route device info...
    if(isset($route->getParams()['device'])){
        $device = $route->getParams()['device'];//set the device sent from the route.
    }
    if (isset($_SESSION['userid']) && !empty($_SESSION['userid'])) {
        //Do database lookup on userID and set to global $user
        //echo "User Found";
        $user = User::find(array('id' => $_SESSION['userid']));//sets the user super global variable from the database accordingly.
    }else{
        //echo "User Not Found<br/>";
    }
}

通过使用全局变量来跟踪设备,然后我可以在$validate_user行中的下一个函数中通过声明global来读取它。