【问题标题】:Calling a method of a controller in Altorouter在 Altorouter 中调用控制器的方法
【发布时间】:2017-01-03 17:24:23
【问题描述】:

我一直在尝试使用 altorouter 获取 Home 控制器的 index() 方法,但无法。我搜索了几个地方,但找不到任何帮助。

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

目录中的主控制器>控制器目录。

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

任何人都可以使用或曾经使用过此 altorouter 指南吗?

附:我在 startup.php 文件中有自动加载功能(包含在 index.php 的顶部)

【问题讨论】:

  • 您的请求是什么样的?
  • @hd,这应该是家....在我的 xampp 上它是“localhost/demo”

标签: php model-view-controller router altorouter


【解决方案1】:

这是旧线程,但这可以帮助您。您需要以不同的方式匹配请求:

<?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
    }
}

【讨论】:

    猜你喜欢
    • 2016-07-13
    • 2018-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多