首页 > 解决方案 > Altorouter - 如何在 php 文件中重定向?

问题描述

我正在使用 Altorouter,我正在尝试使用以下命令重定向到登录路由:$router->generate('authentication'

这是我的 index.php 中的代码:

    <?php
require_once './config/routes.php';
require_once './src/models/AutoloadClass.php';
require_once './src/controllers/AutoloadController.php';
$_SESSION["isAuthenticated"] = false;
?>

<?php

$match = $router->match();
var_dump($match);
if (!is_array($match)) {
    echo "404 Not Found";
    return false;
}

// First connection -> Redirect to authentication route
if ($match['target'] == "root") {
    header($router->generate('authentication'));
    return false;
}
var_dump($match);
if ($match['target'] == "authentication") {
    include("authentication.php");
    return false;
}

if (strpos($match['target'], "#")) { // Check if it's a controller or not.
    list($controller, $action) = explode('#', $match['target']);
    if (is_callable(array(new $controller(), $action))) {
        call_user_func_array(array(new $controller(), $action), array($match['params']));
    } else {
        echo "500";
    }
} else { // It's a view
    $params = $match['params'];
    ob_start();
    require("./src/views/{$match['target']}/{$match['target']}.php");
    $pageContent = ob_get_clean();
    include("layout.php");
}

?>

这是我的 routes.php 代码:

<?php
require './vendor/AltoRouter/AltoRouter.php';

$uri = $_SERVER['REQUEST_URI'];
$router = new AltoRouter();
/* $router->setBasePath('/'); */ 

// Views
$router->map('GET', '/', 'root', 'root');
$router->map('GET', '/authentication', 'authentication', 'authentication');
$router->map('GET', '/home', 'home', 'home');
$router->map('GET', '/contact/[i:id]', 'contact', 'contact');
$router->map('GET', '/organisation/[i:id]', 'organisation', 'organisation');

// Route to controllers
$router->map('GET', '/controller/contact/[i:id]', 'ContactController#Get', 'ContactController');
$router->map('GET', '/controller/organisation/[i:id]', 'OrganisationController#Get', 'OrganisationController');
$router->map('POST', '/controller/adresse', 'AdresseController#GetAll', 'AdresseController');
$router->map('GET', '/controller/coordonne', 'CoordonneController#GetAll', 'CoordonneController');

如何在我的 index.php 中从 / (root) 重定向到 /authentication?

感谢您的帮助

标签: phpaltorouter

解决方案


推荐阅读