1

使用 AltoRouter,我需要将任何/customer以某个path/to/CustomerController.php文件开头的请求传递,然后在那里进行所有特定的匹配。

CustomerController.php我会匹配我所有的方法,即:

public static function Transfer(){... this will be invoked from /customer/transfer...
public static function Register(){... this will be invoked from /customer/register...

在 Laravel 中,你可以这样做:

Route::controller("customer", 'CustomerController');

我需要完全相同的东西,但使用 AltoRouter。我找不到任何方法来做到这一点

http://altorouter.com/

(我只是不想让一个路由文件处理我网站上的所有控制器方法,但让每个控制器处理所有它的特定路由方法)

4

1 回答 1

0

我在文档中发现以下代码片段可能对您有所帮助:

// map users details page using controller#action string
$router->map( 'GET', '/users/[i:id]/', 'UserController#showDetails' );

如果那没有帮助,您可以查看我的路由器Sail。我构建它是为了让程序员以更加面向对象的方式来构建他们的 API。

编辑

这是一个如何使用 Sail 解决此问题的示例。

use Sail\Sail;
use Sail\Tree;
use Sail\Exceptions\NoSuchRouteException;
use Sail\Exceptions\NoMiddlewareException;
use Sail\Exceptions\NoCallableException;

require '../vendor/autoload.php';

$sail = new Sail();

class UserController extends Tree {

    public function build () {
        $this->get('transfer', function($request, $response) {
            self::transfer($request, $response);
        });

        $this->get('register', function($request, $response) {
            self::register($request, $response);
        });
    }

    public static function transfer(&$request, &$response) {
        //do your stuff
    }

    public static function register(&$request, &$response) {
        //do your stuff
    }
}

$sail->tree('customer', new UserController());

try {
    $sail->run();
} catch (NoSuchRouteException $e) {
    echo $e->getMessage();
} catch (NoMiddlewareException $e) {
    echo $e->getMessage();
} catch (NoCallableException $e) {
    echo $e->getMessage();
}
于 2016-03-25T10:46:28.263 回答