{"id":17176370,"url":"https://github.com/phalapi/fast-route","last_synced_at":"2025-04-13T17:09:41.717Z","repository":{"id":57038468,"uuid":"98433597","full_name":"phalapi/fast-route","owner":"phalapi","description":"PhalApi 2.x 扩展类库 - FastRoute路由，可以通过配置实现自定义路由配置，从而轻松映射到PhalApi中的service接口服务。 ","archived":false,"fork":false,"pushed_at":"2021-05-15T01:58:45.000Z","size":23,"stargazers_count":6,"open_issues_count":5,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-27T07:51:45.044Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"agpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/phalapi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-07-26T14:43:04.000Z","updated_at":"2024-03-26T08:55:41.000Z","dependencies_parsed_at":"2022-08-24T14:52:53.131Z","dependency_job_id":null,"html_url":"https://github.com/phalapi/fast-route","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phalapi%2Ffast-route","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phalapi%2Ffast-route/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phalapi%2Ffast-route/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phalapi%2Ffast-route/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/phalapi","download_url":"https://codeload.github.com/phalapi/fast-route/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248480437,"owners_count":21110937,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-15T00:00:02.449Z","updated_at":"2025-04-13T17:09:41.684Z","avatar_url":"https://github.com/phalapi.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n## PhalApi 2.x 扩展类库：基于FastRoute的快速路由\n\n此扩展基于 [FastRoute](https://github.com/nikic/FastRoute) 实现，需要 **PHP 5.4.0** 及以上版本，可以通过配置实现自定义路由配置，从而轻松映射到PhalApi中的service接口服务。  \n  \n## 安装\n\n在项目的composer.json文件中，添加：\n\n```\n{\n    \"require\": {\n        \"phalapi/fast-route\": \"dev-master\"\n    }\n}\n```\n\n配置好后，执行composer update更新操作即可。\n  \n## 配置\n\n我们需要在 **./config/app.php** 配置文件中追加以下配置：\n```php\n\t/**\n\t * 扩展类库 - 快速路由配置\n\t */\n    'FastRoute' =\u003e array(\n         /**\n          * 格式：array($method, $routePattern, $handler)\n          *\n          * @param string/array $method 允许的HTTP请求方式，可以为：GET/POST/HEAD/DELETE 等\n          * @param string $routePattern 路由的正则表达式\n          * @param string $handler 对应PhalApi中接口服务名称，即：?service=$handler\n          */\n        'routes' =\u003e array(\n            array('GET', '/site/index', 'Site.Index'),\n            array('GET', '/examples/curd/get/{id:\\d}', 'Examples_CURD.Get'),\n        ),\n    ),\n\n\n```\n  \n## nginx的协助配置（省略index.php）\n\n如果是使用nginx的情况下，需要添加以下配置：\n\n```\n    # 最终交由index.php文件处理\n    location / {\n        try_files $uri $uri/ $uri/index.php?$query_string;\n    }\n\n    # 匹配未找到的文件路径\n    if (!-e $request_filename) {\n        rewrite ^/(.*)$ /index.php/$1 last;\n    }\n```\n然后重启nginx。  \n  \n  \n## 入门使用\n### (1)入口注册\n```php\n//$ vim ./public/index.php\nrequire_once dirname(__FILE__) . '/init.php';\n\n//显式初始化，并调用分发\n\\PhalApi\\DI()-\u003efastRoute = new PhalApi\\FastRoute\\Lite();\n\\PhalApi\\DI()-\u003efastRoute-\u003edispatch();\n\n$pai = new \\PhalApi\\PhalApi();\n$pai-\u003eresponse()-\u003eoutput();\n```\n  \n## 调用效果及扩展\n### (1)通过新的路由正常访问\n在完成上面的配置后，我们就可以这样进行页面访问测试：\n```\n  http://library.phalapi.com/site/index\n  等效于：http://library.phalapi.com/?service=Site.Index\n \n  http://library.phalapi.com/examples/curd/get/1\n  等效于：http://library.phalapi.com/?service=Examples_CURD.Get\u0026id=1\n```\n \n### (2)非法访问\n当请求的HTTP方法与配置的不符合时，就会返回405错误，如我们配置了：\n```php\narray('POST', '/user/{id:\\d+}/{name}', 'handler2'),\n```\n但是通过GET方式来访问，即：\n```\nhttp://library.phalapi.com/user/123/name\n```\n则会返回：\n```\n{\n    \"ret\": 405,\n    \"data\": [],\n    \"msg\": \"快速路由的HTTP请求方法错误，应该为：POST\"\n}\n```\n\n### (3)路由配置错误\n当在./config/app.php的文件里配置错误的路由时，会直接抛出FastRoute\\BadRouteException异常，以及时提示开发人员修正。\n  \n### (4)异常错误处理器\n我们也可以实现```PhalApi\\FastRoute\\Handler```接口来自定义我们自己的错误异常处理回调函数。如：\n```php\n\u003c?php\nuse PhalApi\\FastRoute\\Handler;\nuse PhalApi\\Response;\n\nclass MyHandler implements Handler {\n\n    public function excute(Response $response) {\n        // ... ...\n    }\n}\n```\n然后，在分发时指定handler：\n```php\n\\PhalApi\\DI()-\u003efastRoute-\u003edispatch(new MyHandler());\n```\n\n### 更多路由配置说明\n请访问 [FastRoute](https://github.com/nikic/FastRoute) ，查看其官方说明。\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphalapi%2Ffast-route","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphalapi%2Ffast-route","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphalapi%2Ffast-route/lists"}