can someone please put this in english for me PHP Cakephp
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);
}
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 :)
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..
上一篇: 如何启用PHP短标签?