通过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>

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...