AltoRouter重定向显示Twig模板

时间:2016-08-11 10:36:30

标签: php twig altorouter

我使用AltoRouter和Twig模板。我的index.php是

的index.php

require('vendor/autoload.php');

$router = new AltoRouter();
$router->map('GET','/', function(){
     require __DIR__ . '/views/index_v.php';
}, 'inicio');   

$router->map('GET','/historia', function(){
     require __DIR__ . '/views/historia_v.php';
}, 'historia');

$match = $router->match();

if( $match && is_callable( $match['target'] ) ) {               
    call_user_func_array( $match['target'], $match['params']); 
} else {
    header( $_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found');
}

它完美无缺,但我想更改我的index.php以使用$ match [' name']或其他参数来调用,例如redirect.php

index.php(已更改)

require('vendor/autoload.php');

$router = new AltoRouter();
$router->map('GET','/', /views/redirect.php', 'inicio');    
$router->map('GET','/historia', /views/redirect.php', 'historia');  

$match = $router->match();

if( $match && is_callable( $match['target'] ) ) {               
    call_user_func_array( $match['target'], $match['params']); 
} else {
    header( $_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found');
}

redirect.php

$loader = new Twig_Loader_Filesystem(array('views', 'includes', 'includes/languages/en', 'includes/languages/es', 'includes/languages/it', 'includes/languages/zh'));

$twig = new Twig_Environment($loader);

include('includes/languages/'.idioma().'/comunes.php'); 
include('includes/languages/'.idioma().'/historia.php');    

if($match['name'] == 'inicio'){
    echo $twig->render('inicio.php', array('textos'=>$textos['idioma']));   
}elseif($match['name'] == 'historia'){
    echo $twig->render('historia.php', array('textos'=>$textos['idioma'], 'historia'=>$historia['idioma']));
}

我的想法是使用一个redirect.php文件来显示一个或另一个Twit模板

有可能吗?

1 个答案:

答案 0 :(得分:0)

我用单个index.php

解决了我的问题

<强>的index.php

require('vendor/autoload.php');

$router = new AltoRouter();
$router->map('GET','/', /views/inicio.php', 'inicio.php');    
$router->map('GET','/historia', /views/historia.php', 'historia.php');  

$loader = new Twig_Loader_Filesystem(array('views', 'includes', 'includes/languages/en', 'includes/languages/es', 'includes/languages/it', 'includes/languages/zh'));

$twig = new Twig_Environment($loader);

include('includes/languages/'.idioma().'/comunes.php'); 

$match = $router->match();

if( $match && is_callable( $match['target'] ) ) {               
    echo $twig->render($match['name'], array('textos'=>$textos['idioma']));
} else {
    header( $_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found');
}