在静态方法中使用session(cakephp 4和3.6+)

If I try this is a class with static methods:

public static function chkLog($role)
{
    $userrole = $this->getRequest()->getSession()->read('userrole');
   return $userrole;
   /// more code

Even tried:

    public static function chkLog($role)
{
    $userrole = Session::read('userrole');
   return $userrole;
   /// more code

In laravel I can:

public static function userRole($role = null)
{
    $userrole = Auth::user()->role;
    $checkrole = explode(',', $userrole);
    if (in_array($role, $checkrole)) {
        return $role;
    }
    return false;
}

usage

public function scopegetPets($query, $petsearch = '')
{
    $petsearch = $petsearch . "%";
    $query = Pet::where('petname', 'like', $petsearch);
    if (ChkAuth::userRole('admin') === false) {
        $userid = Auth::user()->id;
        $query->where('ownerid', '=', $userid);
    }
    $results = $query->orderBy('petname', 'asc')->paginate(5);
    return $results;
}

But in cakephp to call statically I had to write this:

    public static function __callStatic($method, $params)
{
    $instance = ChkAuth::class;
    $c = new $instance;
    return $c->$method(...array_values($params));
}

In order to call this:

public function userRole($role = null)
{
    $userrole = $this->getRequest()->getSession()->read('userrole');
    $checkrole = explode(',', $userrole);
    if (in_array($role, $checkrole)) {
        return $role;
    }
    return false;
    // more

And usage:

   public function getPets($offset = "", $rowsperpage = "", $petsearch = "")
{
    $pagingQuery = "LIMIT {$offset}, {$rowsperpage}";
    $petsearch = $petsearch . "%";
    if (Auth::chkRole('admin') === true) {
        return DB::select("SELECT * FROM " . PREFIX . "pets " . $pagingQuery);
    }
    $authid = Auth::authId();
    return DB::select("SELECT * FROM " . PREFIX . "pets WHERE ownerid = :authid " . $pagingQuery, ["authid" => $authid]);
}

So basically I am trying to figure out how to use session in cake php inside a static method.

Note the __callStatic works, but as a work a round.

CakePHP's Router class includes a static function to return the request object. The request object includes a function to return the session. So:

Router::getRequest()->session()

or, as of version 3.5:

Router::getRequest()->getSession()