REST 风格的 URLS 和 PHP

2022-01-04 00:00:00 rest get php

我很难理解如何使用 REST 风格的 URL.我能得到一些帮助吗?现在我有一个像这样的查询字符串:

I'm having a really hard time getting my head around using REST-style URLS. Can I get a little help with this? Right now I have a query string like so:

example.com/calendar_expanded?date=1270094400

我有一个隐藏 .php 扩展名的 mod 重写.我如何

I have a mod rewrite that's hiding the .php extension. How do I

  1. 让这个calendar_expanded/date/1270094400成为现实.
  2. $_GET url 之后的值?
  1. Make this calendar_expanded/date/1270094400 happen.
  2. $_GET the values from the url after the fact?

我喜欢它背后的概念,但每天工作 16 小时以满足整个月的最后期限对我的大脑造成了伤害.

I love the concept behind it but working 16-hour days to meet a deadline for an entire month is taking a toll on my brain.

感谢您的帮助.

推荐答案

如果您想创建相当多的 REST URL,您应该考虑使用一个 PHP 框架,该框架利用了 前端控制器 设计模式.

If you want to create quite a lot REST URLs you should consider to use a PHP framework that makes use of the Front Controller design pattern.

这样,每个请求都由框架处理,您可以灵活地设计 URL,但它们应该是什么样子.

This way, every request is processed by the framework and you are flexible to design your URLs however they should look like.

例如Symfony 支持开箱即用的 RESTful 设计.

E.g. Symfony supports a RESTful design out of the box.

顺便说一句,RESTful URL 只有在参数以某种方式固定并指定资源(例如博客文章的标题)时才有意义.如果您只想将参数传递给例如控制表的排序(即某物的视图),那么这应该作为普通"GET 参数进入查询字符串.

BTW RESTful URLs only make sense if the parameters are somehow fixed and specify a resource, for example the title of a blog post. If you want to pass a parameter only to e.g. control the sorting of a table (i.e. the view of something) then this should go as a "normal" GET parameter into the query string.

就您而言,您似乎想控制日历中应显示的日期.我个人只会在这里使用普通的 GET 查询字符串.

In your case it looks like you want to control what day should be shown in a calendar. I personally would just use the normal GET query string here.

相关文章