Redirection in laravel without return statement

后端 未结 3 1740
伪装坚强ぢ
伪装坚强ぢ 2021-02-06 03:47

i have this blogsController, the create function is as follows.

public function create() {
  if($this->reqLogin()) return $this->reqLogin();
  return View:         


        
3条回答
  •  无人共我
    2021-02-06 04:19

    It's not a best practice to use this method, but to solve your question "redirect laravel without return statement", you can use this gist

    Create a helper function like:

    if(!function_exists('abortTo')) {
      function abortTo($to = '/') {
        throw new \Illuminate\Http\Exception\HttpResponseException(redirect($to));
      }
    }
    

    then use it in your code:

    public function reqLogin(){
      if(!Auth::check()){
        abortTo(route('login'));
      }
    }
    
    public function create() {
      $this->reqLogin();
      return View::make('blogs.create');
    }
    

提交回复
热议问题