jet/router

用于构建RESTful Web应用程序的简单包

dev-master 2017-09-26 14:18 UTC

This package is not auto-updated.

Last update: 2024-09-28 13:30:16 UTC


README

Travis status

Jet Router

Jet Router包帮助您创建具有命名参数和可测试URL的RESTful应用程序

安装

只需下载git存储库并获取自动加载。或者手动加载文件。但强烈推荐获取自动加载文件(真的,手动加载所有文件太疯狂了)。

Jet/Router将很快发布到composer。

如何使用

在使用此路由器之前,您需要创建一个.htaccess文件,将所有uri重定向到您的frontController或用于路由的任何其他内容(将index.php替换为您的文件)

RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]

要使用此路由器包,只需创建您的URL即可

<?php

namespace Your\Namespace;

use Jet\Router\Router;

$router = new Router(array(
    '/' => function(){
        //Do something here on the default URL
    },
    'error' => function($error){
        //Do something when url don't exists
    },
    'article/[page]:num' => '\Your\Namespace\Class:method',
));

//Or add url via addRoutes

$router->addRoutes(array(
    'article/:slug/[id]:num' => function($id) {
        //Do something with article ID
    }
));

//And now, launch your router !

$router->launch();

您也可以将多个functionclass添加到一条路由中

<?php

namespace Your\Namespace;

use Jet\Router\Router;

$router = new Router(array(
    '/' => array(
        function(){
            //Do something here on the default URL
        },
        function(){
            //Do something else
        },
        '\Your\Namespace\Class:method',
        '\Your\Other\Namespace\Class:method'        
    )
));

在路由中使用模式

  • :any = 允许在URL中包含任何字符
  • :slug = 允许slugify文本(this-is-a-tile-2493/dfds_fds)
  • :alpha = 允许字母字符
  • :num = 允许数字

任何模式都可以命名并使用相同的名称在请求的函数/方法中:[name]:pattern

有用提示

Jet/Router提供一些工具来检测XHR请求和方法类型

  • Router::isXHR() : 检测uri是否通过XHR请求调用
  • Router::getMethod(): 请求URL所使用的的方法

测试

只需运行phpunit或查看travis状态

请随意贡献!

  • 分支
  • 报告错误
  • 帮助开发
  • 给我买一台新mac(什么?)

许可证

BSD许可证下发布