有人可以请把这个英文给我PHP PHP CakePHP

我很抱歉成为这样一个愚蠢的屁股。 我正在学习一个教程,将管理路由添加到我试图创建的cakephp应用程序中作为学习练习。

该教程没有得到很好的解释(我认为我实际上只是一个初学者太多),我不明白以下内容,请问任何人都可以用英文告诉我这里发生了什么。

public function isAuthorized() {
        $role = $this->Auth->user('role');
        $neededRole = null;
        $prefix = !empty($this->params['prefix']) ? $this->params['prefix'] : null;
        if (!empty($prefix) && in_array($prefix, Configure::read('Routing.prefixes'))) {
            $neededRole = $prefix;  
        }
        return (empty($neededRole) || strcasecmp($role, 'admin') == 0 || strcasecmp($role, $neededRole) == 0);
    }

你有什么问题?

你可以一一调试

// This method provides information of role about the currently authenticated user.
  $role = $this->Auth->user('role'); 

// you first check with var_dump($this->params['prefix']) and see the result

/*
 * this line use ternary operator, its say $this->params['prefix'] is not empty 
 * then set $prefix = $this->params['prefix'] otherwise set $prefix=null
*/
  $prefix = !empty($this->params['prefix']) ? $this->params['prefix'] : null;

/*
 *Now check the array
 *echo "<pre>";
 * print_r(Configure::read('Routing.prefixes'));
 * echo "</pre>";   
 * now below line said if `$prefix` is not empty then search that `$prefix` 
 * value in this array `Configure::read('Routing.prefixes')` and if it 
 * exist in the array then set  `$neededRole = $prefix;
 */ 

if (!empty($prefix) && in_array($prefix, Configure::read('Routing.prefixes'))) {
            $neededRole = $prefix;  
        }
/* below line say say that if $role == admin then return $role or return $neededRole */
return (empty($neededRole) || strcasecmp($role, 'admin') == 0 || strcasecmp($role, $neededRole)

参考

  • in_array

  • 三元运算符

  • 乐于帮助 :)


    public function isAuthorized() {
    

    //获取当前用户的角色(admin,user,editor,visitor等)。
    $ role = $ this-> Auth-> user('role');
    //分配一个空值
    $ neededRole = null;
    //获取前缀的参数并分配给$前缀,如果未找到,则分配null
    $ prefix =!empty($ this-> params ['prefix'])? $ this-> params ['prefix']:null;
    //如果$ prefix不为空&如果$ prefix有路由,则将$ prefix分配给$ neededRole
    if(!empty($ prefix)&& in_array($ prefix,Configure :: read('Routing.prefixes'))){$ neededRole = $ prefix;
    } return(empty($ neededRole)|| strcasecmp($ role,'admin')== 0 || strcasecmp($ role,$ neededRole)== 0); }
    //剩下的我不太确定..

    链接地址: http://www.djcxy.com/p/10081.html

    上一篇: can someone please put this in english for me PHP Cakephp

    下一篇: How to get location of Tweets with Twitter search API