Laravel redirection from private method

一个人想着一个人 提交于 2019-12-10 13:01:48

问题


I have a controller that has a method. The code is too long in the method, so I have put some of the codes in other private methods, so that methods become understandable and not make a mess out of it.

Now, when I access the public method from the URL, depending on parameters, it will call a specific private method to process the job. After the job is processed, I want to redirect to a URL, but the redirection is not working.

A sample of my code is as follows:

class SomeClass extends BaseController{
    public function getMethodName()
        {
            //check the params and choose a private method to call
            $this->processJob();
        }
    private function processJob()
    {
         //process the job and redirect at the end
         return Redirect::to('some/url');
    }
}

The problem is, the above redirect does not work. Why is that? In Codeigniter, when you used redirect it works nomatter where it is called from.

If the above code sample is not the right way to do it, would appreciate if someone could show me how it is to be done. Thanks.


回答1:


You have to return the return from $this->processJob() too.

class SomeClass extends BaseController{
  public function getMethodName()
  {
      //check the params and choose a private method to call
      return $this->processJob();
  }

  private function processJob()
  {
     //process the job and redirect at the end
     return Redirect::to('some/url');
  }
}



回答2:


You can try to redirect to another page from your public function, according to your private function result (I think it's a better solution to make your code more human readable). But it could works like you wrote it ...



来源:https://stackoverflow.com/questions/17926159/laravel-redirection-from-private-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!