drrqwokuz71031449 2017-10-14 07:26
浏览 158
已采纳

如何修复无限重定向循环?

I'm running my own MVC structure and I've ran into an issue that I can't figure out how to get around or fix. I've looked at various other questions like 1, 2, and many others which didn't quite help.

I have a table in the database that returns a user's information, if the information isn't found then redirect them to a page to force them to submit the data.

I created a Authenticated class which extends the controller (contains the redirect method):

abstract class Authenticated extends \Core\Controller {

    protected function before() {
        $this->requirePersonality();
    }

    protected function requirePersonality(){
        if(!(new PlannerAuth())->getPersonality()){
            $this->redirect('/app/personalization');
        }
    }
}

In my App Controller I've got:

class App extends Authenticated {

    protected function before(){
        parent::before();
    }

    public function personalizationAction(){
        View::renderTemplate('App/personalization.html');
    }
}

The before() method will run before any and every action method, so when I run any url in the application the following logic happens:

Authentication before() runs, redirects me to /app/personalization, once there, it checks the current before(), which reruns the authentication before() method again, and on and on.. creating an infinite loop.

I've also got an after() method, which will run after all of the methods, but that won't work since it won't force the user onto the personalization page.

This will only happen when I run any method in the App controller, I could have the personalizationAction in another controller, but that's not what I need.

  • 写回答

1条回答 默认 最新

  • douxishai8552 2017-10-14 07:39
    关注

    You shouldn't have a parent::before(); calling in the before function of your /app/personalization controller. You can have an implementation of requirePersonality() inside that class but avoiding the redirection to the same controller.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么