简体   繁体   English

在Altorouter中调用控制器的方法

[英]Calling a method of a controller in Altorouter

I have been trying to get the index() method of the Home controller using the altorouter but am unable to. 我一直在尝试使用altorouter获取Home控制器的index()方法,但无法这样做。 I have searched a few places but I could not find any help. 我已经搜索了几个地方,但找不到任何帮助。

Here is the index.php 这是index.php

<?php

include 'system/startup.php';
include 'library/AltoRouter.php';

// Router
$router = new AltoRouter();
// Change here before upload
$router->setBasePath('/demo');
$router->map('GET|POST','/', 'home#index', 'home');

// match current request
$match = $router->match();

if( $match && is_callable( $match['target'] ) ) {
    call_user_func_array( $match['target'], $match['params'] ); 
} else {
    // no route matched
    header( $_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found');
}

the home controller in the catalog>controller directory. 目录> controller目录中的主控制器。

<?php
class home {
    public function index() {
        echo 'home';
    }
}

Can anyone using or who ever used this altorouter guide?. 任何人都可以使用或曾经使用过此altorouter指南吗?

PS I have the autoload function in the startup.php file (included at the top of the index.php) PS我在startup.php文件中具有自动加载功能(包含在index.php的顶部)

This is old thread but this can help you. 这是旧线程,但可以为您提供帮助。 You need match request in different way: 您需要以其他方式进行匹配请求:

<?php

include 'system/startup.php';
include 'library/AltoRouter.php';

// Router
$router = new AltoRouter();
$router->setBasePath('/demo');
$router->map('GET|POST','/', 'home#index', 'home');
$match = $router->match();

if ($match === false) {
    // here you can handle 404
} else {
    list( $controller, $action ) = explode( '#', $match['target'] );
    if ( is_callable(array($controller, $action)) ) {
        call_user_func_array(array($controller,$action), array($match['params']));
    } else {
        // here your routes are wrong.
        // Throw an exception in debug, send a  500 error in production
    }
}

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

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