Git Product home page Git Product logo

Comments (4)

lloydzhou avatar lloydzhou commented on June 6, 2024

controller确实应该放在不同的文件夹,至于你说的处理的资源一样,那就是操作相同的model嘛!这有什么难以理解的?

from phpboot.

dearbird avatar dearbird commented on June 6, 2024

如果是MVC模式的话,这样处理是没问题的,但是以phprs的规则来看,似乎并没有对嵌套资源的情况有优化。
按我的理解, /users/100/Devices 和 /catalogs/200/Devices 两个路由应该放在同一个类里面,但是它们的url第一段是不一样的,因此有困惑。

from phpboot.

 avatar commented on June 6, 2024

如果要套用MVC模式,可以考虑将users和catalogs当做Controller,Devices当做model。
当然,如果只是想把 /users/100/Devices 和 /catalogs/200/Devices 放在一个类里,也是有办法的。

可以定义一个如Devices类:

/** @path("/")*/
class Devices{
      /**@route({"GET","/users/\*/devices"})*/
      public function getUserDevices....

      /** @route({"GET","/catalogs/\*/Devices"})*/
      public function geCatalogDevices....
}

同时Users和Catalogs中其他方法的路由不变。phprs的路由是以方法为单位,而不是以类为单位的,路由搜索时优先匹配精度高的规则。

举例来说,如果存在Devices和Users两个类,分别定义了/users//devices和/users//info接口,那么/users/id/devices会命中Devices中的方法,/users/id/info会命中Users中的方法

from phpboot.

dearbird avatar dearbird commented on June 6, 2024

作为轻量级的解决方案, 显然 @caoym 方案要好一些,但还是有一些问题。
以答复中Devices的例子, 如果path 指定为 “/”, 会导致后面每个方法的 route声明都要带 /devices

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.