ThinkPHP6使用Base公共控制器redirect()不起作用的解决方法

作者: 温新

分类: 【ThinkPHP】

阅读: 8041

时间: 2020-04-20 16:16:50

无论是什么样的系统,大多数都会有用户登录功能,这是非常常见的功能,只要你是开发人员就会碰到。

作为最常见的功能,ThinkPHP6似乎更像出现了BUG一样。当使用Base公共控制器判断没有登录后,使用redirect()进行重定向,此时问题就来了。

redirect()不起作用!对于刚刚接触ThinkPHP6的开发人员来说,可能会认为这是ThinkPHP6的BUG。我个人赞同这样的认为!

事实上,这不是ThinkPHP6的BUG,此时你可能会发一个黑人问号的图片过来,但事实就是如此,这不是BUG。但是,作为框架,希望ThinkPHP官方人员能够改一改。

相比于Laravel框架,ThinkPHP6与之有着不小的差距。号称最优雅的PHP框架,Laravel已经为开发准备好了常用的功能,简单的户系统登录只需要执行一个简单的指令即可。

而ThinkPHP6确实弱了不少。

说了这么多的废话,下面开始代码解决问题吧!

【错误一】redirect()不起作用

public function index()
{
    $test = session('user');
    if(empty($test)){
        // 此处黑人问号,今年然不起作用
        $this->redirect('admin/Login/index');
    }
}

【错误二】重写initialize方法后,出现重定向次数过多的问题

谷歌浏览器:该网页无法正常运作company.com 将您重定向的次数过多。

火狐浏览器:Firefox 检测到该服务器正在将指向此网址的请求无限循环重定向。

【解决方法】这两个问题同时解决,方法如下

下面的文件都在 app/admin/controller

Base.php // 继承BaseController的公共控制器

<?php

namespace app\admin\controller;

use app\BaseController;
use think\App;
use think\exception\HttpResponseException;
use think\facade\Session;

/**
 * Class Base 公共控制器
 * @package app\admin\controller
 */
class Base  extends  BaseController
{

    // 初始化控制器。重要的操作一
    public function initialize()
    {
        parent::initialize();

        // 用户未登录重定向到登录界面
        if(empty($this->isLogin())){
            // 正确重定向方法,一定写url()
            return $this->redirectTo(url('login/index'));

            // 错误重定向,没有写url()
            // return $this->redirectTo('admin/Login/index');
        }
    }

    /**
     * 判断用户是否登录
     * @return bool
     */
    public function isLogin()
    {
        $adminData = Session::get('adminData');
        // 用户未登录返回 false
        if(empty($adminData)){
            return false;
        }

        // 用户已登录返回 true
        return true;
    }

    /**
     * 自定义重定向方法 重要的操作二
     * @param $args
     */
    public function redirectTo(...$args)
    {
        // 此处 throw new HttpResponseException 这个异常一定要写
        throw new HttpResponseException(redirect(...$args));
    }
}

Login.php

<?php

namespace app\admin\controller;

use think\facade\Db;
use think\facade\Session;
use think\facade\View;
use think\Request;

/**
 * Class Login 后台登录
 * @package app\admin\controller
 */
class Login extends Base
{
    public function initialize() {
        // 用户若已经登录,则重定向到首页
        if($this->isLogin()) {
            return $this->redirectTo(url('index/index'));
        }
    }
}

这样就解决了登录重定向的问题了。 此处呢,只给出登录后再次访问登录页面后重定向到首页的方法

总结:这个问题不是BUG的原因是什么?原因是redirect()返回的是\think\response\Redirect对象,而此对象被app\BaseController类中的构造方法所获取。

vendor/topthink/framework/src/helper.php

if (!function_exists('redirect')) {
    /**
     * 获取\think\response\Redirect对象实例
     * @param string $url  重定向地址
     * @param int    $code 状态码
     * @return \think\response\Redirect
     */
    function redirect(string $url = '', int $code = 302): Redirect
    {
        return Response::create($url, 'redirect', $code);
    }
}

app/BaseConatroller.php

/**
 * 构造方法
 * @access public
 * @param  App  $app  应用对象
 */
public function __construct(App $app)
{
    $this->app     = $app;
    $this->request = $this->app->request;

    // 控制器初始化
    $this->initialize();
}

我是夕阳何处寻,期待和优秀的你一起同行!

夕阳何处寻

2020年04月21日

请登录后再评论