thinkphp-jump icon indicating copy to clipboard operation
thinkphp-jump copied to clipboard

Call to a member function isJson() on null

Open zhizhistar opened this issue 4 years ago • 14 comments

6.0.2各种报错::: 1、Call to a member function isJson() on null 2、Call to a member function isAjax() on null 3、....

zhizhistar avatar Apr 24 '20 14:04 zhizhistar

6.0.2各种报错::: 1、Call to a member function isJson() on null 2、Call to a member function isAjax() on null 3、....

怎么用的?怎么可能各种报错

liliuwei avatar Apr 24 '20 14:04 liliuwei

安装首页安装教程来的 先composer安装,然后在基类use引入,然后在自定义控制器集成基类,然后使用$this->error(),success();然后就报错。代码如下: 基类: use \liliuwei\think\Jump;

控制器: `use app\BaseController; use think\facade\Session;

class Base extends BaseController { public function index(){}

//  检查用户是否登录
public function isLogin(){
    $uid = Session::get('uid');
    dump($uid);
    if(empty($uid)) {
        return $this->error('xxx');
    }

}`

zhizhistar avatar Apr 24 '20 14:04 zhizhistar

安装首页安装教程来的 先composer安装,然后在基类use引入,然后在自定义控制器集成基类,然后使用$this->error(),success();然后就报错。代码如下: 基类: use \liliuwei\think\Jump;

控制器: `use app\BaseController; use think\facade\Session;

class Base extends BaseController { public function index(){}

//  检查用户是否登录
public function isLogin(){
    $uid = Session::get('uid');
    dump($uid);
    if(empty($uid)) {
        return $this->error('xxx');
    }

}`

不存在你说的问题,其他人都是正常的

liliuwei avatar Apr 24 '20 15:04 liliuwei

6.0.2各种报错::: 1、Call to a member function isJson() on null 2、Call to a member function isAjax() on null 3、....

同样的问题

贴代码

liliuwei avatar Jun 01 '20 05:06 liliuwei

我也是同样的问题 thinkphp6.0.5

haozhi6 avatar Jan 03 '21 09:01 haozhi6

定义一个方法: public function verification($jwt) {

    try {
        throw new \think\Exception('access_token已弃用!');

        $decoded = JWT::decode($jwt.'123', $this->key, ['HS256']); //HS256方式,这里要和签发的时候对应            
        $arr = (array)$decoded;

        $data = (array)$arr['data'];
        if (!Cache::get('admin_' . $data['admin_uid'])) { //手动退出过的情况,使用未过期的旧的access_token
            throw new \think\Exception('access_token已弃用!');
        }
        return $arr;
    } catch (\Firebase\JWT\SignatureInvalidException $e) {  //签名不正确

        $this->error('签名不正确,非法令牌', NULL, [
            'code' => Config::get('errormsg.illegal_token'),
            'data' => []
        ]);
    } catch (\Firebase\JWT\BeforeValidException $e) {  // 签名在某个时间点之后才能用
        $this->error('令牌还未生效', NULL, [
            'code' => Config::get('errormsg.token_ineffective'),
            'data' => []
        ]);
    } catch (\Firebase\JWT\ExpiredException $e) {  // token过期
        $this->error('令牌已过期', NULL, [
            'code' => Config::get('errormsg.token_expired'),
            'data' => []
        ]);
    } catch (\think\Exception $e) {  //其他错误
        $this->error($e->getMessage(), NULL, [
            'code' => Config::get('errormsg.error'),
            'data' => []
        ]);
    }
}

在应用中间件中调用: $JwtToken = new JwtToken(); $JwtToken->verification($access_token); 报错:Call to a member function isJson() on null 或者是 Call to a member function isAjax() on null

haozhi6 avatar Jan 03 '21 09:01 haozhi6

定义一个方法: 公共功能验证($ jwt) {

    try {
        throw new \think\Exception('access_token已弃用!');

        $decoded = JWT::decode($jwt.'123', $this->key, ['HS256']); //HS256方式,这里要和签发的时候对应            
        $arr = (array)$decoded;

        $data = (array)$arr['data'];
        if (!Cache::get('admin_' . $data['admin_uid'])) { //手动退出过的情况,使用未过期的旧的access_token
            throw new \think\Exception('access_token已弃用!');
        }
        return $arr;
    } catch (\Firebase\JWT\SignatureInvalidException $e) {  //签名不正确

        $this->error('签名不正确,非法令牌', NULL, [
            'code' => Config::get('errormsg.illegal_token'),
            'data' => []
        ]);
    } catch (\Firebase\JWT\BeforeValidException $e) {  // 签名在某个时间点之后才能用
        $this->error('令牌还未生效', NULL, [
            'code' => Config::get('errormsg.token_ineffective'),
            'data' => []
        ]);
    } catch (\Firebase\JWT\ExpiredException $e) {  // token过期
        $this->error('令牌已过期', NULL, [
            'code' => Config::get('errormsg.token_expired'),
            'data' => []
        ]);
    } catch (\think\Exception $e) {  //其他错误
        $this->error($e->getMessage(), NULL, [
            'code' => Config::get('errormsg.error'),
            'data' => []
        ]);
    }
}

在应用中间件中调用: $ JwtToken = new JwtToken(); $ JwtToken-> verification($ access_token); 报错:在null上调用成员函数isJson()或在null上调用成员函数isAjax()

我想请问下你是怎么解决这么问题的

endLife888 avatar Apr 09 '21 02:04 endLife888

已在BaseControoler中use \liliuwei\think\Jump;还是跳转报错,请问下到底是何原因? #0 [0]Error in Jump.php line 163 Call to a member function isJson() on null }

/**
 * 获取当前的response 输出类型
 * @access protected
 * @return string
 */
protected function getResponseType()
{
    return $this->request->isJson() || $this->request->isAjax() ? 'json' : 'html';
}

}

codelibo avatar May 10 '21 08:05 codelibo

控制器中要使用 public function initialize(){ 。。。。 }

用__construct就会报错。

hellojcl avatar Dec 13 '21 01:12 hellojcl

是的,项目做一大半了。用到__construct的都报错了不能调用这个插件了。有大神解决吗?

Py9595 avatar Jan 05 '22 08:01 Py9595

是的,项目做一大半了。用到__construct的都报错了不能调用这个插件了。有大神解决吗?

建议再熟悉一下trait 的用法,了解一下优先级和冲突的解决

手册地址:https://www.php.net/manual/zh/language.oop5.traits.php

liliuwei avatar Jan 05 '22 11:01 liliuwei

是的,项目做一大半了。用到__construct的都报错了不能调用这个插件了。有大神解决吗?

可以考虑用框架带的initialize() 代替 __construct来实现一些功能

liliuwei avatar Jan 06 '22 04:01 liliuwei

image

use \liliuwei\think\Jump;

class Auth
{
   use Jump
  public function __construct()
  { }
    public function checkNode() 
    {
      $this->error('请先登录', url('admin/login/index'));
    }

ThinkPHP V6.0.12LTS

FastGUI avatar Jun 21 '22 08:06 FastGUI

同样出现了此问题,求解

78778443 avatar Mar 14 '23 02:03 78778443