加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

通过Php AltoRouter路由

发布时间:2020-12-13 13:06:42 所属栏目:PHP教程 来源:网络整理
导读:我试图第一次使用路由器(AltoRouter),我无法调用任何页面. Web文件夹结构 代码 的index.php require 'lib/AltoRouter.php';$router = new AltoRouter();$router-setBasePath('/alto');$router-map('GET|POST','/','home#index','home');$router-map('GET|POS
我试图第一次使用路由器(AltoRouter),我无法调用任何页面.

Web文件夹结构


代码

的index.php

require 'lib/AltoRouter.php';

$router = new AltoRouter();
$router->setBasePath('/alto');
$router->map('GET|POST','/','home#index','home');
$router->map('GET|POST','display.php','display');
$router->map('GET','/plan/','plan.php','plan');
$router->map('GET','/users/',array('c' => 'UserController','a' => 'ListAction'));
$router->map('GET','/users/[i:id]','users#show','users_show');
$router->map('POST','/users/[i:id]/[delete|update:action]','usersController#doAction','users_do');
// match current request
$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');
}

我在计划文件夹中有一个名为plan.php(显示计划)的文件,我正在尝试的超链接

<a href="<?php echo $router->generate('plan'); ?>">Plan <?php echo $router->generate('plan'); ?></a>

这不起作用.

你能帮我吗?

您不能通过将plan.php作为参数传递给匹配函数来调用plan.php

检查http://altorouter.com/usage/processing-requests.html处的示例

如果要使用plan.php中的内容

你应该使用以下格式的地图

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

到文件计划/ plan.php添加echo’测试计划’;

另外,仔细检查.htaccess文件是否包含

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]

此外,如果您使用$router-> setBasePath(‘/ alto’)设置基本路径;你的index.php文件应放在alto目录中,这样你的网址就是这样的http://example.com/alto/index.php

工作范例:

require 'lib/AltoRouter.php';

$router = new AltoRouter();
$router->setBasePath('/alto');

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

// match current request
$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');
}

然后这将工作得很好

<a href="<?php echo $router->generate('plan'); ?>">Plan <?php echo $router->generate('plan'); ?></a>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读