简体   繁体   English

通过Php AltoRouter进行路由

[英]Routing via Php AltoRouter

I am trying to use a router (AltoRouter) for the first time and am unable to call any page. 我正在尝试首次使用路由器(AltoRouter),并且无法调用任何页面。

Web folder structure Web文件夹结构

在此处输入图片说明 The Code 代码

Index.php 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 我在计划文件夹中有一个名为plan.php(显示计划)的文件,而我正在尝试的超链接是

<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 您不能通过将plan.php作为参数传递给match函数来调用plan.php

Check examples at http://altorouter.com/usage/processing-requests.html http://altorouter.com/usage/processing-requests.html上查看示例

If you want to use content from plan.php 如果您想使用plan.php中的内容

you should use map in the following format 您应该使用以下格式的map

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

to the file plan/plan.php add echo 'testing plan'; 在文件plan/plan.php添加plan/plan.php echo 'testing plan';

Also, double check that your .htaccess file contains 另外,请仔细检查您的.htaccess文件是否包含

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

Also, if you set base path with $router->setBasePath('/alto'); 另外,如果您使用$router->setBasePath('/alto');设置基本路径$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 您的index.php文件应放置在alto目录中,因此在这种情况下,您的网址应为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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM