我试图第一次使用路由器(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处的示例
你应该使用以下格式的地图
$router->map('GET',function() { require __DIR__ . '/plan/plan.PHP'; },'plan');
另外,仔细检查.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>