8

我第一次尝试使用路由器(AltoRouter)并且无法调用任何页面。

网页文件夹结构

在此处输入图像描述 编码

索引.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>

这是行不通的。

你能帮我吗?

4

1 回答 1

4

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

在http://altorouter.com/usage/processing-requests.html查看示例

如果您想使用 plan.php 中的内容

您应该使用map以下格式

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

在文件中plan/plan.php添加echo 'testing plan';

此外,请仔细检查您的 .htaccess 文件是否包含

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

此外,如果您使用文件设置基本路径,$router->setBasePath('/alto');则应index.php将其放置在alto目录中,以便您的 url 在这种情况下http://example.com/alto/index.php

工作示例:

require 'lib/AltoRouter.php';

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

$router->map('GET','/plan/',  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>
于 2016-08-04T20:16:10.010 回答