php - 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?
感谢您的帮助
解决方案
推荐阅读
- javascript - 如何从我的 Babel 编译代码中删除 transform typeof 符号代码?
- javascript - 通过javascript从时间轴获取笔记输入控件
- c - 进程的退出状态
- php - 你能将 Symfony 配置为使用 XML 而不是 YAML 和注解吗?
- javascript - 我应该如何在 JavaScript 中存储新创建的元素?
- github - 用于访问私有存储库的 Github 个人访问令牌:/repos/:user/:repo/contents
- xamarin.forms - 为什么在使用 Prism 在 Xamarin.Forms 中导航到 root 时,我的 NavigationPage 顶部会出现默认标题?
- javascript - 使用 JavaScript 如何通过 ID 获取此 HTML 元素内部的字符串?
- linux - 在 Crontab 中删除现有作业/任务
- c - Arm Gcc 弱别名重定义