0

我正在使用带有 Twig 模板的 AltoRouter。我的 index.php 是

索引.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');
}

重定向.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']));
}

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

可能吗?

4

1 回答 1

0

我用一个 index.php 解决了我的问题

索引.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');
}
于 2016-08-19T20:34:36.680 回答