laravel 没有返回语句的laravel中的重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25581353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:01:26  来源:igfitidea点击:

Redirection in laravel without return statement

phplaravel

提问by anwerj

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

我有这个 blogsController,创建函数如下。

public function create() {
  if($this->reqLogin()) return $this->reqLogin();
  return View::make('blogs.create');
 }

In BaseController, i have this function which checks if user is logged in.

在 BaseController 中,我有这个功能来检查用户是否登录。

    public function reqLogin(){
      if(!Auth::check()){
        Session::flash('message', 'You need to login');
        return Redirect::to("login");
      }
    }

This code is working fine , but it is not what is need i want my create function as follows.

这段代码工作正常,但它不是我想要的创建函数,如下所示。

public function create() {
  $this->reqLogin();
  return View::make('blogs.create');
 }

Can i do so?

我可以这样做吗?

Apart from that, can i set authantication rules , like we do in Yiiframework, at the top of controller.

除此之外,我可以像在Yii框架中那样在控制器顶部设置身份验证规则吗?

采纳答案by Laurence

You should put the check into a filter, then only let the user get to the controller if they are logged in in the first place.

您应该将支票放入过滤器中,然后只让用户在首先登录的情况下访问控制器。

Filter

筛选

Route::filter('auth', function($route, $request, $response)
{
    if(!Auth::check()) {
       Session::flash('message', 'You need to login');
       return Redirect::to("login");
    }
});

Route

路线

Route::get('blogs/create', array('before' => 'auth', 'uses' => 'BlogsController@create'));

Controller

控制器

public function create() {
  return View::make('blogs.create');
 }

回答by tacone

Beside organizing your code to fit better Laravel's architecture, there's a little trick you can use when returning a response is not possible and a redirect is absolutely needed.

除了组织您的代码以更好地适应 Laravel 的架构之外,还有一个小技巧可以在无法返回响应并且绝对需要重定向时使用。

The trick is to call \App::abort()and pass the approriate code and headers. This will work in most of the circumstances (excluding, notably, blade views and __toString()methods.

诀窍是调用\App::abort()并传递适当的代码和标头。这将适用于大多数情况(特别是刀片视图和__toString()方法除外。

Here's a simple function that will work everywhere, no matter what, while still keeping your shutdown logic intact.

这是一个简单的函数,它可以在任何地方使用,无论如何,同时仍然保持你的关闭逻辑完整

/**
 * Redirect the user no matter what. No need to use a return
 * statement. Also avoids the trap put in place by the Blade Compiler.
 *
 * @param string $url
 * @param int $code http code for the redirect (should be 302 or 301)
 */
function redirect_now($url, $code = 302)
{
    try {
        \App::abort($code, '', ['Location' => $url]);
    } catch (\Exception $exception) {
        // the blade compiler catches exceptions and rethrows them
        // as ErrorExceptions :(
        //
        // also the __toString() magic method cannot throw exceptions
        // in that case also we need to manually call the exception
        // handler
        $previousErrorHandler = set_exception_handler(function () {
        });
        restore_error_handler();
        call_user_func($previousErrorHandler, $exception);
        die;
    }
}

Usage in PHP:

在 PHP 中的用法:

redirect_now('/');

Usage in Blade:

在刀片中的使用:

{{ redirect_now('/') }}