can someone please put this in english for me PHP Cakephp

余生长醉 提交于 2020-03-26 03:00:12

问题


I'm sorry for being such a dumb ass. I'm following a tutorial to add admin routing to my cakephp application that I'm trying to create as a learning exercise.

The tutorial isn't hugely well explained (I think I'm just too much of a beginner in reality) and I don't understand the following, could anyone please tell me in english what is happening here.

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);
    }

回答1:


where u have probelm???

u can debug one by one

// 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)

Reference

  • in_array

  • ternary Operator

Happy To Help :)




回答2:


public function isAuthorized() {

//fetches the current user's role (admin,user,editor,visitor,etc..)
$role = $this->Auth->user('role');
//assigns a null value
$neededRole = null;
//fetches the param for prefix and assigns to $prefix,if not found,assigns null
$prefix = !empty($this->params['prefix']) ? $this->params['prefix'] : null;
//if $prefix not null & if $prefix has route configured assign $prefix to $neededRole
if (!empty($prefix) && in_array($prefix, Configure::read('Routing.prefixes'))) { $neededRole = $prefix;
} return (empty($neededRole) || strcasecmp($role, 'admin') == 0 || strcasecmp($role, $neededRole) == 0); }
//the rest i'm not too sure..



来源:https://stackoverflow.com/questions/5662242/can-someone-please-put-this-in-english-for-me-php-cakephp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!