如何重定向到某个 url 并将值返回给调用者函数?

发布于 2024-12-26 11:31:05 字数 315 浏览 2 评论 0原文

我正在 PHP 中开发 joomla MVC 组件。 我需要以下功能 1) & 2)在我的组件代码中的 model.php 之一中。

function xyz()
{
 //blah blah
 //1) I have to redirect
 $mainframe->redirect( 'index.php?option=com_abc','');
 //2) and also return value to caller function 
 return $row; 
}

我该如何实现这一目标?

谢谢 :)

I am working on a joomla MVC component in PHP.
I need following functionality 1) & 2) in one of model.php in my component code.

function xyz()
{
 //blah blah
 //1) I have to redirect
 $mainframe->redirect( 'index.php?option=com_abc','');
 //2) and also return value to caller function 
 return $row; 
}

How do I achieve this ?

Thanks :)

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

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

发布评论

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

评论(1

想念有你 2025-01-02 11:31:05

通常,在 MVC 方法中,模型不应该重定向用户,这是在控制器上完成的。在控制器上,您可以这样做:

$this->setRedirect($url, $optional_message);

// some more code to be executed

这样,用户将在当前控制器的操作完成后重定向,而不是在调用 setRedirect 方法时重定向,

但如果您确实需要在模型上执行此操作,则可以传递控制器作为模型函数的参数,并从那里设置重定向并返回值:

<?php
function xyz($controller)
{

    $controller->setRedirect( 'index.php?option=com_abc','');

    return $row; 
}
?>

然后,在您的控制器中:

$model->xyz($this);

Usually, in the MVC approach, models shouldn't be redirecting the users, that's done on the controllers. On the controller, you can do:

$this->setRedirect($url, $optional_message);

// some more code to be executed

In that way, the user will be redirected after the current controller's action is completed and not exactly when you call the setRedirect method

but if you really need to do it on the model, you can pass the controller as a parameter there to the model's function and set the redirect from there and return the value:

<?php
function xyz($controller)
{

    $controller->setRedirect( 'index.php?option=com_abc','');

    return $row; 
}
?>

then, in your controller:

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