slimframework3中的PHP作用域
我认为这可能是一个简单的问题。有人能帮我确定如何最好地访问每个路由器功能中的slimframework3中的PHP作用域,php,slim,Php,Slim,我认为这可能是一个简单的问题。有人能帮我确定如何最好地访问每个路由器功能中的$route变量吗?我正在尝试从一个阵列中添加许多Slim 3路由器功能。以下代码来自/src/routes.php: <?php // Standard CRUD Routes (based on templates) $app->group('/api/v1', function () use ($app) { $routes = [ 'calendar' => [ 't
$route
变量吗?我正在尝试从一个阵列中添加许多Slim 3路由器功能。以下代码来自/src/routes.php:
<?php
// Standard CRUD Routes (based on templates)
$app->group('/api/v1', function () use ($app) {
$routes = [
'calendar' => [
'table' => 'calendar',
'prefix' => 'cal',
'sort' => 'cal_name_orig',
'fields' => ['cal_name_orig','cal_name_tran','cal_public','cal_color'],
'permRead' => 1,
'permWrite' => 1
],
'classroom' => [
'table' => 'classroom',
'prefix' => 'room',
'sort' => 'room_name',
'fields' => ['room_owner_p_id','room_name','room_common','room_capacity_normal','room_capacity_max','room_current_desks'],
'permRead' => 1,
'permWrite' => 1
]
];
// register all standard routes using above data
foreach ($routes as $key => $route) {
echo "<br><br>ROUTE: $key<br>";
print_r($route);
// Add Route: retrieve all records
$app->get('/' . $key . 's', function ($request, $response, $args) {
return $this->common->getAll($route['table'], $route['prefix'], $route['sort'], $route['permRead']);
});
// Add Route: retrieve specific record
$app->get('/' . $key . '/[{id}]', function ($request, $response, $args) {
return $this->common->getById($route['table'], $route['prefix'], $args['id'], $route['permRead']);
});
// Add Route: create new record
$app->put('/' . $key . '/new', function ($request, $response, $args) {
return $this->common->putNew($route['table'], $route['prefix'], $route['fields'], $route['permWrite']);
});
// Add Route: update specific record
$app->post('/' . $key . '/[{id}]', function ($request, $response, $args) {
return $this->common->postById($route['table'], $route['prefix'], $args['id'], $route['fields'], $route['permWrite']);
});
// Add Route: delete specific record
$app->delete('/' . $key . '/[{id}]', function ($request, $response, $args) {
return $this->common->deleteById($route['table'], $route['prefix'], $args['id'], $route['permWrite']);
});
}
});
试试看
这非常有效。我已经使用PHP很多年了。。。但从未理解“使用”关键字。因此,这基本上将作用域引入函数?是的,简单地说:“使用”使使用外部变量成为可能:)
$app->put('/' . $key . '/new', function ($request, $response, $args) use ($route) {