action-hook插件,beforeAction继承时出现 return response 无效的问题。

roczyl

Common中:
public function beforeAction(Request $request)
{
$config = get_site_config();
$h = date('H', time());
/ 站点关闭公告 /
if ($config['siteopen'] == 0) {
return redirect('/index/base/closed');
}
}

Index中:
class Index extends Common
public function beforeAction(Request $request)
{
parent::beforeAction($request);
}

public function test(Request $request)
{
return response('I\'m test');
}

发现当访问 Index/test 的时候,并未执行 Common 中的 redirect,执行了Index中的test方法。

如果将 return redirect('/index/base/closed') 放到 Index 的beforeAction,却又能够执行。

求解决方案。

682 2 0
2个回答

ichynul
$response = parent::beforeAction($request);
if($response)
{
    return $response;
}
  • roczyl 2022-08-10

    还是无效,并没有阻止后面的输出

  • roczyl 2022-08-10

    测试了一下,parent::beforeAction($request) 返回的是 NULL

  • ichynul 2022-08-10

    parent::beforeAction不一定有返回,if ($config['siteopen'] == 0) 这个条件成立吗

  • roczyl 2022-08-10

    搞定了,非常感谢。

2548a

这不就对了嘛,问问题你把东西都贴上来才行呀,不然谁会去回你问题.问题出在index类的 beforeAction 方法上,你仅仅只是调用了 parent::beforeAction($request); 是没用的,这个如果有响应你是要return 回去才会处理的.

  • roczyl 2022-08-10

    嗯。明白了,第一次接触,很多思维要改过来。

年代过于久远,无法发表回答
🔝