MVC/HMVC/PAC 模式中 URL 结构的约定是什么?

发布于 2024-10-17 00:46:50 字数 1654 浏览 12 评论 0原文

在 MVC 中,它就像 http://www.yourdomain.com/sampleController/sampleAction/ ,如果您只调用 /sampleController/ 那么 /sampleController/indexAction/< /code> 如果你只调用 / 那么 /indexController/indexAction/ 会触发。

当然也有例外,但这或多或少是惯例。

Zend 有一些类似的东西。他们称之为模块。

它们基本上只是包含一个 MVC 逻辑的文件夹。所以你可以调用/Module1/Controller/Action/。如果您只调用 /Module1/ ,则 /Module1/indexController/indexAction/ 会触发。如果您有一个巨大的项目,这会很方便,因为您可以构建更多的项目,但如果您只有一个小项目,那就很烦人了。

所以我真的很喜欢 HMVC/PAC 的想法,并且想在我的框架中采用它。

我是否正确理解它与 Zend 基本相同但具有无限的嵌套模块?

例如我有 /sub-project/sub-sub-project/controller/action/

如果我调用/A/B/C/D/,约定是什么?

这是否意味着模块 A/B 中控制器 C 中的操作 D? 或者模块 A/B/C 中控制器 D 中的 IndexAction?

让我们举个例子:

Content
    ToplistController 
        AdministrateAction
        IndexAction
ContentController
     ToplistAction
Users
    Chat
        RoomController
            IndexAction

我现在调用 URL /content/toplist/

对于 URL /users/chat/room/?room=1 这个示例很明显,因为只有一种可能性。但这是正确的吗?是否有一个约定可以唯一地解决正确控制器中的正确操作?

我的第一个想法是“尽可能少地猜测”。

所以我首先检查 url 是否直接与操作匹配。

如果有一个控制器/模块与索引操作调用相同,那么如果存在“更高级别”的匹配,它就无法触发。

如果不是这种情况,我会查看 url 是否直接与控制器匹配并附加 IndexAction。

如果情况并非如此,我会寻找一个模块并猜测 IndexController 和 IndexAction,如果情况并非如此,我会寻找一个名为 index 的模块。

但我想避免这种 if/else 的东西和文件系统访问。所以我想知道公约是怎样的。或者甚至有一个吗?我找不到任何例子!

或者,如果没有指定,则调用 IndexAction 之类的事情只是没有完成,但每个“短网址”都必须在单独的路由逻辑中指定?

或者我完全误解了 HMVC/PAC 的概念?

仅供参考:我包含了 php 标签,因为我正在 php 中构建我的框架,并且想了解 php 中的约定。我经常看到其他编程语言的差异。

In MVC its like http://www.yourdomain.com/sampleController/sampleAction/ and if you call just /sampleController/ then /sampleController/indexAction/ and if you just call / then /indexController/indexAction/ fires.

Of course there are exceptions but thats more or less the convention.

Zend has something smilir. They call it Modules.

They are basically just folders that contain an MVC logic each. So you can call /Module1/Controller/Action/. If you just call /Module1/ then /Module1/indexController/indexAction/ fires. Its convinient if you have a huge project because you can structure even more but its annoying if you have just a little project.

So I really like the Idea of HMVC/PAC and want to adopt it in my framework.

Did I understand it correctly that its basically the same as Zend does but with unlimited nested modules?

So for example I have /sub-project/sub-sub-project/controller/action/ ?

And what is the convention if I call /A/B/C/D/.

Does it mean Action D in controller C in module A/B?
Or IndexAction in Controller D in Module A/B/C?

Lets put it in an example:

Content
    ToplistController 
        AdministrateAction
        IndexAction
ContentController
     ToplistAction
Users
    Chat
        RoomController
            IndexAction

I now call the URL /content/toplist/.

For the URL /users/chat/room/?room=1 the example makes it obvious because there is only one possibility. But is it the right one? Is there a convention to uniquely adress the right action in the right controller?

My first idea was to "guess as little as possible".

So I first check if the url is directly matched to an action.

And if there is a controller/module called the same with an index action, it just can not fire if a match on a "higher level" is present.

If this is not the case I see whether the url directly matches a controller and append IndexAction.

If this is not the case too I look for a module and guess IndexController and IndexAction and if this is not the case I look for an module called index.

But i would like to avoid this if/else stuff and the file system accesses. So I wondered how the convention is. Or is there even one? I could not find any examples!

Or does something like calling IndexAction if none is specified just not done but every "short url" has to be specified in seperate routing logic?

Or have I completely mis understood the concept of HMVC/PAC?

FYI: I included the php tag because I am doing my framework in php and want to know about the conventions in php. I have often seen differences in other programming languages.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风筝有风,海豚有海 2024-10-24 00:46:50

一个简单的(我认为被广泛接受的)约定是存在/不存在尾部斜杠,其中 A/B/C/D/ 表示控制器 D 中的 IndexAction,A/B/C/D 表示控制器 C 中的操作 D 。

One simple (and widely accepted, I believe) convention is presence/absence of a trailing slash, where A/B/C/D/ would imply IndexAction in Controller D and A/B/C/D would imply Action D in Controller C.

负佳期 2024-10-24 00:46:50

没有独立于框架的官方约定。

URI 标识资源。某些框架如何将这些 URI 映射到内部应用程序功能取决于该框架。例如,如果您想做 REST,则根本不需要执行任何操作,因为 操作由所使用的 HTTP 动词暗示,例如

DELETE http://example.com/resourceName/1234

,“路由”框架中的相同操作可能类似于

POST http://example.com/resourceName/delete (with POST Body 1234)

两个 URI 以某种方式映射到应用程序中知道如何执行操作的机制删除。

There is no official framework-independent convention.

URIs identify resources. How some framework maps those URIs to internal application functions is up to the framework. For instance if you want to do REST, you wont have actions at all, because actions are implied by the used HTTP verbs, e.g.

DELETE http://example.com/resourceName/1234

whereas the same in a "routed" framework could be something like

POST http://example.com/resourceName/delete (with POST Body 1234)

with both URIs somehow mapping to the mechanism in your application that knows how to delete.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文