我有以下内容:
// Other code leading up to this..
....
//
$validPassword = password_verify($pass, $user['password']);
if($validPassword){
$_SESSION['user_id'] = $user['username'];
$_SESSION['logged_in'] = time();
// redirects in this case
header( "Location: /wherever.php" );
die();
} else{
die('Wrong password!');
}
handleRoutes函数应该采用路线名称(例如“ about”或“ contact”)并将其传递给树枝渲染器。但是,$ twig在handleRoutes函数中不可用,而且我不知道如何正确地将对象传递给它。我尝试过:
<?php
require __DIR__ . "/vendor/autoload.php";
$router = new AltoRouter();
$loader = new Twig_Loader_Filesystem( array( 'views', 'views/pages', 'views/partial' ) );
$twig = new Twig_Environment( $loader, array(
'cache' => 'tmp',
'debug' => true,
'auto_reload' => true
) );
function handleRoutes($name) {
echo $twig->render($name . '.twig');
}
$router->map( 'GET', '/[*:id]', function ($id) {
handleRoutes($id, $twig);
});
$match = $router->match();
if( $match && is_callable( $match['target'] ) ) {
call_user_func_array( $match['target'], $match['params'] );
} else {
// no route was matched
header( $_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found');
}
?>
但是随后$ twig也无法用于$ router-> map中的功能。
答案 0 :(得分:1)
您可以使用函数use
向closures
传递变量,例如
$router->map( 'GET', '/[*:id]', function ($id) use ($twig) {
handleRoutes($id, $twig);
});