我是cakephp的新手。我正在建立一个网站,没有用户层次结构,只有管理员和公共用户。我想禁止公共用户输入特定静态页面。当然,该页面位于视图/页面中,因此其视图/页面/adminpanel.ctp。请指定我应该提供的代码。预先感谢您
这是我的显示功能
function __checkLayout($pageName)
{
//$pageName = "";
$temp = "";
switch ($pageName)
{
case "home":
$temp = "atheer";
break;
case "":
$temp = "atheer";
break;
case "adminpanel":
$temp = "adminview";
break;
}
return $temp;
}
public function display() {
$path = func_get_args();
//$this->layout='atheer';
//$this->layout = Configure::read('layout.'.$page);
$count = count($path);
if (!$count) {
return $this->redirect('/');
}
$page = $subpage = $title_for_layout = null;
if (!empty($path[0])) {
$page = $path[0];
}
if (!empty($path[1])) {
$subpage = $path[1];
}
if (!empty($path[$count - 1])) {
$title_for_layout = Inflector::humanize($path[$count - 1]);
}
$this->layout = $this->__checkLayout($page);
$this->set(compact('page', 'subpage', 'title_for_layout'));
try {
$this->render(implode('/', $path));
} catch (MissingViewException $e) {
if (Configure::read('debug')) {
throw $e;
}
throw new NotFoundException();
}
}
好的,如果是这样,请在cakephp中使用特定页面,
请查看修改的代码:
public $allowedPages = array('page1', 'page2'); //here you add allowed pages only
public function beforeFilter() {
$this->Auth->allow('display');
}
function __checkLayout($pageName)
{
//$pageName = "";
$temp = "";
switch ($pageName)
{
case "home":
$temp = "atheer";
break;
case "":
$temp = "atheer";
break;
case "adminpanel":
$temp = "adminview";
break;
}
return $temp;
}
public function display() {
$path = func_get_args();
//$this->layout='atheer';
//$this->layout = Configure::read('layout.'.$page);
$count = count($path);
if (!$count) {
return $this->redirect('/');
}
$page = $subpage = $title_for_layout = null;
if (!empty($path[0])) {
$page = $path[0];
}
if (!empty($path[1])) {
$subpage = $path[1];
}
if (!empty($path[$count - 1])) {
$title_for_layout = Inflector::humanize($path[$count - 1]);
}
$this->layout = $this->__checkLayout($page);
if(!in_array($page, $this->allowedPages) && !$this->Auth->login()) {
return $this->redirect('/login'); //here redirects to login page change the path if the path is different
}
$this->set(compact('page', 'subpage', 'title_for_layout'));
try {
$this->render(implode('/', $path));
} catch (MissingViewException $e) {
if (Configure::read('debug')) {
throw $e;
}
throw new NotFoundException();
}
}
希望它有帮助
您可以将所有公开访问的方法放在$this->Auth->allow('func1', 'func2'...)
;
如果要允许用户可用的所有方法,请使用$this->Auth->allow('*')
。
请参阅文档