PHP AltoRouter将路由完全传递给控制器

时间:2016-03-24 21:28:03

标签: php altorouter

使用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/

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

1 个答案:

答案 0 :(得分: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();
}