求帮忙分析一下yii2 Helper:: checkRoute() 框架源代码

/**

 * Check access route for user.
 * @param string|array $route
 * @param integer|User $user
 * @return boolean
 */
public static function checkRoute($route, $params = [], $user = null)
{
    $config = Configs::instance();
    $r = static::normalizeRoute($route);
    if ($config->onlyRegisteredRoute && !isset(static::getRegisteredRoutes()[$r])) {
        return true;
    }

    if ($user === null) {
        $user = Yii::$app->getUser();
    }
    $userId = $user instanceof User ? $user->getId() : $user;

    if ($config->strict) {
        if ($user->can($r, $params)) {
            return true;
        }
        while (($pos = strrpos($r, '/')) > 0) {
            $r = substr($r, 0, $pos);
            if ($user->can($r . '/*', $params)) {
                return true;
            }
        }
        return $user->can('/*', $params);
    } else {
        $routes = static::getRoutesByUser($userId);
        if (isset($routes[$r])) {
            return true;
        }
        while (($pos = strrpos($r, '/')) > 0) {
            $r = substr($r, 0, $pos);
            if (isset($routes[$r . '/*'])) {
                return true;
            }
        }
        return isset($routes['/*']);
    }
}
阅读 3.2k
1 个回答

你这不太好分析,因为这时用户自己写的而不是框架里的,而且调用了各种其它自定义的方法。大体来说就是判定用户是否可以使用此路由的一个权限判定的方法。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题