通过 PHP AltoRouter 路由 [英] Routing via Php AltoRouter

查看:43
本文介绍了通过 PHP AltoRouter 路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次尝试使用路由器 (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 = $router->match();if( $match && is_callable( $match['target'] ) ) {call_user_func_array( $match['target'], $match['params'] );} 别的 {//没有匹配的路由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 作为参数传递给 match 函数来调用 plan.php

http://altorouter.com/usage/processing-requests.html

如果你想使用来自 plan.php 的内容

你应该使用以下格式的map

$router->map('GET','/plan/', function() {需要 __DIR__ .'/计划/计划.php';} , '计划');

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

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

RewriteEngine onRewriteCond %{REQUEST_FILENAME} !-f重写规则.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','/plan/', function() {需要 __DIR__ .'/计划/计划.php';} , '计划');//匹配当前请求$match = $router->match();if( $match && is_callable( $match['target'] ) ) {call_user_func_array( $match['target'], $match['params'] );} 别的 {//没有匹配的路由header( $_SERVER["SERVER_PROTOCOL"] .'404 Not Found');}

这样就可以正常工作了

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

I am trying to use a router (AltoRouter) for the first time and am unable to call any page.

Web folder structure

The Code

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');
}

I have a file named plan.php (display plan) in the plan folder and the hyperlink that I am trying is

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

which does not work.

Can you help?

解决方案

You cannot call plan.php by passing plan.php as an argument to match function

Check examples at http://altorouter.com/usage/processing-requests.html

If you want to use content from plan.php

you should use map in the following format

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

to the file plan/plan.php add echo 'testing plan';

Also, double check that your .htaccess file contains

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

Also, if you set base path with $router->setBasePath('/alto'); your index.php files should be placed in the alto directory so your url would be in that case http://example.com/alto/index.php

Working example:

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');
}

then this will work just fine

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

这篇关于通过 PHP AltoRouter 路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆