GithubHelp home page GithubHelp logo

Comments (13)

 avatar commented on July 19, 2024

考虑路由规则增加正则表达式支持,你觉得如何?

from phpboot.

dearbird avatar dearbird commented on July 19, 2024

目前大部分的框架都支持正则路由, 我认为是必须支持的。

from phpboot.

 avatar commented on July 19, 2024

同意,正在加此功能

from phpboot.

 avatar commented on July 19, 2024

考虑再三,决定先不加正则路由,理由如下:

  1. phprs的定位是开发RESTful接口的框架,对于restful的url,并不提倡定义复杂的路由规则,当前基于层次结构的路由定义已经能够满足
  2. 对于“严格匹配”的需求,可以对原有路由规则实现进行扩展,代价更小。
  3. 对于“正则规则”的路由,自动生成文档将是个问题,将无法把正则转换成可读性好的接口示例。
  4. 性能问题,由于php原生的正则函数不支持批量规则匹配,将导致路由匹配时,需逐条匹配规则。
  5. 对于特殊情况,当前路由规则无法满足的,可以在webserver端配置正则路由并rewrite解决。
  6. 为保持框架简单,非必要的特性,还是不引入为妙。

from phpboot.

dearbird avatar dearbird commented on July 19, 2024

是的,问题不在于使用何种方式实现,而在于接口必须能在url错误的情况下返回404.

from phpboot.

 avatar commented on July 19, 2024

@route()增加一个参数,用于表示是否严格匹配路径
@route({"GET", "/", true}) 表示严格匹配,GET /abc将和此路由不匹配
@route({"GET", "/"}) 默认形式,非严格匹配,GET /abc将和此路由匹配

from phpboot.

dearbird avatar dearbird commented on July 19, 2024

我认为严格匹配模式应该是默认开启的。
API的语义应该是明确的,我不晓得什么场景下会用到不严格匹配的模式?

from phpboot.

 avatar commented on July 19, 2024

默认不严格匹配, 主要是为了向下兼容,另外也是参照nginx location配置规则,如果没有历史包袱,可以考虑默认严格。

典型场景有:

  1. hook, 通常会应用于一类接口
  2. 资源名不确定的情况,比如文件下载的接口,因为文件不固定,所以可以使用@route(GET,/files/) 定义,对应请求可以是/files/123 或者/files/mydir/123等。

from phpboot.

dearbird avatar dearbird commented on July 19, 2024

至少已经能支持严格匹配了,
更进一步的话,能否在配置中使用一个全局开关呢,避免每个路由都要显示配置?

from phpboot.

 avatar commented on July 19, 2024

同意, 已增加配置

"phprs\\Router"=>[
        "properties"=>[
            "default_strict_matching"=>true, //默认是否严格匹配
        ]
    ],

from phpboot.

dearbird avatar dearbird commented on July 19, 2024

hook路由,不应该使用严格匹配。

from phpboot.

 avatar commented on July 19, 2024

我觉得hook路由是不是使用严格匹配,还是由业务决定的,举两个例子

  1. 使用严格匹配的情况
    如果需要只hook创建订单的接口POST /orders/, 此时应该使用严格匹配@route(POST, /orders/, true),否则会hook其他接口,如POST /orders/*
  2. 使用严格匹配的情况
    如果为了增加权限控制,需要hook所有订单相关接口,此时则应该使用非严格匹配

from phpboot.

dearbird avatar dearbird commented on July 19, 2024

我理解的hook,就是为了同时匹配多个接口的。

hook如果只是满足特定的接口,为何不直接与该接口实现放在一起?
可能某些场景下,需要分开实现,目前还没有遇到,暂时保留意见。

from phpboot.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.