php route 路由转发类库 支持composer加载

本文介绍了一个PHP路由转发类库的安装步骤及如何通过Composer进行依赖管理。此外,还提供了Nginx配置示例,展示了如何将PHP请求转发到指定端口,并给出了核心代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

php路由转发类库 支持composer加载

源码及使用demo git地址:点击查看

install

1.添加以下内容至project目录composer.json

添加仓库

"repositories": {
    "packagist": {
      "type": "composer",
      "url": "https://packagist.phpcomposer.com"
    },
    "croute": {
      "type": "vcs",
      "url": "git@github.com:luyunhua/croute.git"
    }
  }

添加依赖

  "require": {
    "php": ">=5.5.9",
    "luyunhua/croute": "dev-master"
  }

2.更新依赖

composer update

using

此处假设大家的php项目支持自动加载,这样你在使用路由分发器的时候将无需在include类库等源文件等操作

nginx 配置

配置server
server {
    listen       82;
    #listen       somename:8080;
    server_name  _;
    index index.php;
    try_files $uri
              $uri/
              @rewrite;

    location @rewrite {
        rewrite ^(.*)$ /index.php?_url=$1;
    }

    location ~ \.php$ {
        root /var/wwwroot/mframe;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }


     error_page   500 502 503 504  /50x.html;
     location = /50x.html {
        root   /usr/share/nginx/html;
     }
}

nginx 将php请求转发至82端口处理

    location ~ \.php$ {
        proxy_pass   http://127.0.0.1:82;
    }

核心代码演示

<?php
/**
 * Created by luyh@59store.com.
 * User: luyh
 * Date: 16/5/30
 * Time: 下午1:42
 * File: index.php
 */
include __DIR__ . '/boots/autoload.php';

$fileRoute = new \Tomato\Route\FileRoute();
$fileRoute->get('/myroute/abc/\d+' ,'App\Ctrl\DefaultController@index');
$fileRoute->post('/myroute/post/\w+/d' ,'App\Ctrl\DefaultController@index2');

$dispatcher = new \Tomato\Route\Dispatcher($fileRoute);
$dispatcher->run();
<?php namespace FastRoute; use PHPUnit\Framework\TestCase; class RouteCollectorTest extends TestCase {     public function testShortcuts() {         $r = new DummyRouteCollector();         $r->delete('/delete', 'delete');         $r->get('/get', 'get');         $r->head('/head', 'head');         $r->patch('/patch', 'patch');         $r->post('/post', 'post');         $r->put('/put', 'put');         $expected = [             ['DELETE', '/delete', 'delete'],             ['GET', '/get', 'get'],             ['HEAD', '/head', 'head'],             ['PATCH', '/patch', 'patch'],             ['POST', '/post', 'post'],             ['PUT', '/put', 'put'],         ];         $this->assertSame($expected, $r->routes);     }路由(routing)是指分组从源到目的地时,决定端到端路径的网络范围的进程[1]  。路由工作在OSI参考模型第三层——网络层的数据包转发设备。路由器通过转发数据包来实现网络互连。虽然路由器可以支持多种协议(如TCP/IP、IPX/SPX、AppleTalk等协议),但是在我国绝大多数路由器运行TCP/IP协议。路由器通常连接两个或多个由IP子网或点到点协议标识的逻辑端口,至少拥有1个物理端口。路由器根据收到数据包中的网络层地址以及路由器内部维护的路由表决定输出端口以及下一跳地址,并且重写链路层数据包头实现转发数据包。路由器通过动态维护路由表来反映当前的网络拓扑,并通过网络上其他路由器交换路由和链路信息来维护路由表。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值