• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Router: 一个简单性感的php router

原作者: [db:作者] 来自: 网络 收藏 邀请

一个简单性感的php router

Install

composer require man0sions/router

Run Demo

 1:git clone https://git.oschina.net/man0sions/Router.git 2:php -S 127.0.0.1:8080 public/index.php  3:在浏览器访问: http://localhost:8080/home 

useage

1:简单用法

$router = \LuciferP\Router\Base\RouterFactory::getRouter();/** * 1:简单用法 * $res->status() 设置返回码[默认200] 200,404,500 ... * $res->type()   设置返回类型[默认 text/html] text/json... * $res->json()   在页面输出json * $res->jsonp()  在页面输出jsonp * $res->render() 把数据渲染到指定的页面 */$router->get('/home', function ($req, $res) {//  $res->status(200)->send(json_encode($req));//  $res->type('text/json')->send(json_encode($req));//  $res->json(['hello'=>'world']);//  $res->jsonp(['hello'=>'world']);//  $res->redirect("http://baidu.com");    $res->status(200)->type('text/html')->render(BASE_PATH . "/../views/view.php", ['name' => 'zhangsan', 'age' => 20]);});$router->run();

1.1 参数解析

/** * 1.1: get参数 */$router->get('/hello/:name', function ($req, $res) {    $query = $req['get'];    $res->json($query);});/** * 1.2 post参数 */$router->post('/hello', function ($req, $res) {    $query = $req['post'];    $res->json($query);});

2:高级用法

/** * 2.1 auth * 用户名密码默认为:admin,admin */$router->auth("/auth", function ($req, $res) {    $name = @$req['server']['PHP_AUTH_USER'];    $passwd = @$req['server']['PHP_AUTH_PW'];    if (!($name == 'admin' && $passwd == 'admin')) {        header('WWW-Authenticate: Basic realm="My Realm"');        header('HTTP/1.0 401 Unauthorized');        exit;    } else {        $res->send("欢迎回来");    }});/** * 2.2 格式化response * html---\LuciferP\Router\ResponseData\HtmlData * json---\LuciferP\Router\ResponseData\JsonData * xml----\LuciferP\Router\ResponseData\XmlData */$router->get('/name/:name/age/:age', function ($req, $res) {    $query = $req['get'];    $xml = $res->dataformat(new \LuciferP\Router\ResponseData\XmlData($query));    $res->type("text/xml")->send($xml);});/** * 2.3 指定所有[get,post]请求"/" */$router->all("/", function ($req, $res) {    $res->send("all page");});

3. controller 使用方法

/** * 3.1 :只渲染包含layout 的 html */$router->get('/home/index', '\LuciferP\Router\Controller\Home@index');/** * 3.2 :调用response渲染数据 */$router->get('/home/index2', '\LuciferP\Router\Controller\Home@index2');/** * 3.3 :把包含在layout 的 html一起渲染的数据交给response返回 */$router->get('/home/index3', '\LuciferP\Router\Controller\Home@index3');

4.最后别忘了加上 $router->run();

$router->run();

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
phalconplus: phaloncplus - a framewok based on phalcon发布时间:2022-02-13
下一篇:
DouPHP: DouPHP的唯一目标就是简洁、易于理解。发布时间:2022-02-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap