微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

通过Php AltoRouter路由

我试图第一次使用路由器(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>

原文地址:https://www.jb51.cc/php/136344.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐