从 URL 获取变量,然后将其传递给控制器​​ CakePHP 2.0

发布于 2025-01-08 15:58:12 字数 321 浏览 0 评论 0原文

我正在尝试为我正在构建的项目构建一种类似 Wordpress 的 CMS 系统。我希望用户能够即时创建页面,并使它们出现在网站的某些区域。

我在 Symfony2 中做了类似的事情,其中​​控制器从 URL 中获取特定变量(如在 Route.yml 文件中定义的,通常是 $id 等),然后我使用控制器中的变量来显示与它相关的任何内容数据库。

然而,我不习惯 CakePHP 2.0,并且正在努力寻找我需要的东西。我知道这是可能的,但我不知道实现它的最佳方法。特别是 CakePHP 使用与 Symfony 不同的路由文件。

如何从 URL 中获取变量并将其传递以在控制器内使用?

I'm trying to build a sort of Wordpress-esque CMS system to a project I'm building. I want the user to be able to create pages on the fly, and for them to appear in certain areas of the website.

I have made something similar in Symfony2, where the controller grabs a specific variable from the URL (as definded in the route.yml file, usually $id etc) and I then use the variable in the controller to display whatever content it relates to in the database.

However, I'm not used to CakePHP 2.0, and struggling to find what I need. I know it's possible, but I don't know the best way to achieve it. Especially as CakePHP uses a different routes file than Symfony.

How would I grab a variable from the URL and pass it for use inside a controller?

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

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

发布评论

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

评论(2

终难愈 2025-01-15 15:58:12

变量是指 GET 查询字符串参数,例如 /foo?key=value 吗?您可以通过请求对象在控制器中访问它们$this->request->query['key']

如果您正在寻找更集成的东西,您可以使用 CakePHP 的默认路由或创建您自己的路由。

默认路由/controller/action/param1/param2 等 URL 配合使用,并按位置将参数传递给操作。例如,/posts/view/521映射到对PostsController中的view(521)的调用,以及/posts/byMonth/ 2012/02 映射到对 byMonth("2012","02") 的调用。

您还可以使用命名参数,URL 类似于 /controller/action/key1:value1/key2:value2。在控制器操作中,您可以使用 $this->params['named']['key1'] 读取它们。

通过自定义路由,您可以将 URL 设为您想要的任何内容。你不会被迫采用 /controller/action 模式;您可以将 /archives/2012-02 映射到 PostsController::byMonth(2012,2),或者使用 /512-post-title映射到 PostsController::view(512)

通常,您会从默认路由开始,并在您决定需要时添加自定义路由。您可以在 http://book.cakephp 中阅读有关默认和自定义路由的所有信息。 org/2.0/en/development/routing.html

By variable do you mean GET query string parameters, like in /foo?key=value? You can access them in the controller through the request object: $this->request->query['key'].

If you are looking for something more integrated you can use CakePHP's default routes or make your own.

The default routes work with URLs like /controller/action/param1/param2 and pass the parameters to the action by position. For instance /posts/view/521 maps to a call to view(521) in PostsController, and /posts/byMonth/2012/02 maps to a call to byMonth("2012","02").

You can also use named parameters and the URLs look like /controller/action/key1:value1/key2:value2. In controller actions you would read them with $this->params['named']['key1'].

With custom routes you can make your URLs anything you want. You're not forced to the /controller/action pattern; you can make /archives/2012-02 map to PostsController::byMonth(2012,2), or have /512-post-title map to PostsController::view(512).

Typically you would start out with the default routes and add custom routes when you decide you need them. You can read all about the default and custom routes in http://book.cakephp.org/2.0/en/development/routing.html

怪我闹别瞎闹 2025-01-15 15:58:12

简短的回答:这取决于。

如果您希望将参数传递给函数,则根本不需要弄乱路由;每个未命名的 URL 路径段都会按顺序处理,并作为方法参数传递给操作(因此 /controller/action/1234 将“1234”作为第一个参数传递给 action controller 控制器类中的 code> 方法)。

命名参数允许您在 URL 字符串中的任何位置传递参数,并使它们成为可选的。表单只是 key:value ,可以通过控制器中的 $this->params['named'] 访问它们。

最后一个选项是前缀路由。快速了解这一点的最佳地点自然是 CakePHP Cookbook,但一般要点是在路由定义中,您可以通过在 URL 中添加冒号前缀来命名路径组件,与默认路由显示 :controller:plugin:action< 的方式相同/代码>。您可以定义任何您喜欢的名称,甚至可以选择应用正则表达式模式要求,然后通过控制器中的 $this->params['variablename'] 访问它。

Short answer: it depends.

If you're looking to pass parameters to a function, you don't need to mess with routes at all; every non-named URL path segment is processed in order and handed to the action as method parameters (so /controller/action/1234 passes "1234" as the first parameter to the action method in the controller controller class).

Named parameters allow you to pass parameters anywhere in the URL string, and to make them optional. The form is just key:value and they're accessed via $this->params['named'] in the controller.

The last option is prefix routing. The best place to get to speed on that is naturally the CakePHP Cookbook, but the general gist is that in a route definition, you can name a path component in the URL by prefixing it with a colon, identically to how the default routes show :controller, :plugin and :action. You can define any name you like, even optionally applying regex pattern requirements and then access it through $this->params['variablename'] in the controller.

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