当前位置:首页 > articles > 正文内容

如何强制从RedirectResponse对象重定向Laravel 5.6?

Luz4周前 (07-20)articles141

How do I force a Laravel 5.6 redirect from a RedirectResponse object?

  • lufc 问题:
    • I have a custom non-Eloquent class called
      Item
      . In the constructor, the
      Item
      attempts to authenticate with my app. I then have an
      Auth()
      method to check whether the authentication was successful, and if not to fire a redirect.
    • public function Auth() {    if (isset($this->authenticated)) {        return $this;    } else {        return redirect('/auth');    }}
    • I use the
      Auth
      method to access
      Item
      statically via a
      Item
      Facade. The intended outcome is that if the Item is authenticated, we proceed to the Index with that Item as a variable. If not the redirect would be triggered.
    • public function index() {    $item = Item::Auth();    return view('index',$item);}
    • However, if not authenticated, all that happens is a Laravel
      RedirectResponse
      object is passed to the index view. How can I force that redirect to actually fire?
    • 我想过但不喜欢的解决方案
    • if ($item instanceOf RedirectResponse)...
      in the controller, but this feels clunky
    • $item = new Item; if ($item->authenticated)...
      this is fine for the controller but I want to trigger the redirect from multiple parts of the app so there would be a lot of code re-use which doesn't feel efficient.
    • 谢谢你的帮助。要么在正面,要么触发重定向对象。
  • 回答:
    • apokryfos - vote: 0
      • You can handle this case by using an
        AuthenticationException
        :
      • public function Auth() {    if (isset($this->authenticated)) {        return $this;    }     throw new AuthenticationException();}
      • The authentication exception will generate either a redirect to login or a 401 error on JSON routes. You can of course throw a different exception and handle it as a redirect in your
        App\Exceptions\Handler
        class
      • 例如:
      • public function Auth() {    if (isset($this->authenticated)) {        return $this;    }     throw new ItemNotAuthorizedException(); //Custom class you need to create}
      • In
        Handler.php
        :
      •  public function render($request, Exception $exception) {    if ($exception instanceof ItemNotAuthorizedException) {       return redirect("/auth");    }    return parent::render($request, $exception); }
      • 注意:Laravel 5.6可能也允许异常实现其自己的渲染功能,但您应该参考文档了解这一点。

相关文章

使用nodejs和mongodb进行前端搜索

Frontend search with nodejs and mongodbCaleb 问题: 我今天刚开始学习mongodb,我有一段代码可以用来查询我的数据库和搜索地址:“Park Lane 38”。这是代码,这是结果:...

单元测试名称重要吗?

Are unit-test names important?Xian 问题: 如果单元测试名称随着时间的推移而变得过时,如果您认为测试本身是最重要的事情,那么选择明智的测试名称是重要的吗?ie[Test]public void Sh...

应该如何使用kotlin将findViewById传递数据从活动页面写入片段页面

How should write the findViewById pass data to Fragment page from Activity page using kotlinMIN LEE 问题: //这是我的活动页面覆盖...

脚本1 linux中的crear usuarios和demás

Script 1 de crear usuarios y demás en linuxMaría Isabel De la Osa Rocha 问题: Cómo se haría el script del usuario porq...

C++:泛型&gt>如何为一个通用函数定义多个模板解题,该函数充当静态实例创建者

C++:Generics >> How to define several template deklarations for a generic function, which serves as an static inst...

Visual Studio GitHub链接到一个陌生人帐户

Visual Studio GitHub is linked to a stranger accountAhmed Khaldoon 问题: 我从VisualStudio2022(我们称之为divice a)中创建了一个新的私有re...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。