Cakephp 从控制器到另一个控制器的内部重定向

发布于 2024-10-04 05:12:08 字数 339 浏览 6 评论 0原文

更新:我写了一些关于在 php 中使用 header 的错误语句;所以忘记那部分:)

我想要的是从另一个控制器获取并显示控制器的视图(带有控制器的数据),而不需要在浏览器中更改 url。

一些细节:

  1. 重定向不能完成这项工作,因为是直接重定向(通过浏览器)
  2. requestAction 不允许我正确获取 css 和图像

我需要这个东西,因为我有一个控制器调度程序,可以在内部重定向到其他控制器。

我认为唯一(正确)的解决方案是在 /config 中使用routes.php 和 Router::connect 并使用调度程序控制器中的逻辑。

Update: i wrote some wrong statements about the use of header in php; so forget that part :)

What i want is to fetch and display a controller's view (with controller's data) from another controller, without have url change in browser.

Some details:

  1. Redirect doesn't do the job because is a direct redirect (via browser)
  2. requestAction doesn't allow me to fetch css and images correctly

I need this thing because i have a controller dispatcher that redirects internally to the other controllers.

I think the only (correct) solution is to use routes.php in /config with Router::connect
and there use the logic that was in the dispatcher controller.

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

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

发布评论

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

评论(2

番薯 2024-10-11 05:12:08

嗯... header() 是用于重定向的函数,除非 PHP 文档是错误的。 (http://php.net/manual/en/function.header.php) cakePHP 的核心使用 header 来实现重定向功能(参见 cake/libs/controller.php 的第 721 - 730 行)。

所以我不确定你的意思是“像普通 PHP 一样”。 CakePHP 是 PHP,它只是构建在面向对象的代码之上。这不是魔法或扭曲的做事方式。因此,要在 cake 中进行重定向,您可以简单地使用:

$this->redirect(array('controller' => 'my_controller', 'action' => 'my_action'));

它将调用 header() 函数。

现在。如果您坚决不使用重定向(也许您要访问外部站点),您可以在代码中调用 header() 。只要确保你输入了 exit();在标头调用之后:

header('Location: http://call/my/url');
exit();

它的工作方式与重定向相同。这只是很多不必要的额外工作。请记住,使用重定向将维护域名并自动为您构建 URL。

ummm... header() is the function to use for a redirect unless the PHP documentation is wrong. (http://php.net/manual/en/function.header.php) The core in cakePHP uses header for the redirect function (see lines 721 - 730 of cake/libs/controller.php).

So I am not certain what you mean "like normal PHP". CakePHP is PHP, it's just built on object oriented code. It's not magic or twisted ways of doing things. So to do a redirect in cake, you can simply use:

$this->redirect(array('controller' => 'my_controller', 'action' => 'my_action'));

And it will call the header() function.

Now. If you are dead set on not using redirect (maybe if you are going to an external site), you can call header() in the code. Just be sure you put the exit(); after the header call:

header('Location: http://call/my/url');
exit();

It will work just the same as redirect. It's just a lot of unnecessary extra work. Keep in mind that using redirect will maintain the domain name and build the URL for you automatically.

街角迷惘 2024-10-11 05:12:08

一般来说,将 URL 连接到控制器是路由的工作。如果您的逻辑相当复杂并且普通路由无法解决它,您甚至可以编写自己的路由解析器类来执行更复杂的逻辑(这全部在手册中)。

如果此路由逻辑涉及数据库查询或任何其他类型的控制器逻辑,并且可能会导致基于某些内部状态的同一 URL 产生非常不同的输出,那么您正在制作一个非常 RESTless 应用程序,我建议您应该重新考虑您要做什么。话虽如此,您可以使用 $this->render() 从任何控制器操作渲染任何视图。每个视图的控制器逻辑可以放入 AppController 中,或者可能(部分)放入从任何地方调用的模型中。因此,路由不是“重定向”到不同的控制器,而是像往常一样路由到特定的控制器操作,该操作动态调用它需要调用的代码,然后渲染它需要渲染的视图。

如果您希望您的应用程序保留在相同的 URL 上但显示截然不同的内容,您可能还应该考虑制作 AJAX 应用程序。

适合您的解决方案可能介于两者之间。

In general, connecting URLs to controllers is the job of routes. If your logic is rather complex and normal routes won't cut it, you can even write your own route parser class that does more complex logic (that's all in the manual).

If this routing logic involves database queries or any other sort of controller logic and may lead to very different output for the same URL based on some internal state though, you're making a very RESTless application and I'd submit you should rethink what you're trying to do. Having said that, you can render any view from any controller action using $this->render(). The controller logic for each view could be put in the AppController or possibly (partly) the models to be called from anywhere. So instead of "redirecting" to a different controller, a route just routes to a specific controller action as usual, that action dynamically calls code it needs to call and then renders the view it needs to render.

If you want your app to stay on the same URL but display very different content, you should probably also look into making an AJAX app.

The right solution for you is probably somewhere in between.

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