简体   繁体   English

(Phalcon)如何从模型中的静态方法重定向(Phalcon \\ Http \\ Response)

[英](Phalcon) How to redirect (Phalcon\Http\Response) from static method in model

I'm trying to create a reusable static function which redirects if true. 我正在尝试创建一个可重用的静态函数,如果为true则会重定向。 This function will be in a model. 此功能将在模型中。

public function checkEmtpy(ResultsetInterface $resultset)
{
    $di = \Phalcon\DI::getDefault();

    if (empty($resultset->toArray())) {

        $di->get('flash')->error('Page not found.');
        return $di->get('response')->redirect('content');

    } else {

        return false;

    }
}

I tried several ways to redirect but I can't get it to redirect from the model. 我尝试了几种方法来重定向,但无法从模型中重定向。

What can I change to make this work or isn't this possible at all? 我可以更改以使其正常工作还是根本不可能?

It is against MVC principle to do redirects in models. 在模型中进行重定向是违反MVC原理的。 The redirect has to be done in the Controller. 重定向必须在Controller中完成。 What you should do is return only the status from your model only. 您应该做的只是仅返回模型的状态。 Something like this: 像这样:

// Model
public function checkEmtpy(ResultsetInterface $resultset)
{
    return empty($resultset->toArray());
}

// Controller
public action someAction()
{
    $isEmpty = (new YourModelName)->checkEmtpy($someVariable);
    if ($isEmpty === true) {
        return $this->response->redirect(...);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM