【发布时间】:2012-02-13 20:25:52
【问题描述】:
==========================================编辑 =====================================
根据查尔斯的建议,我使用以下代码完成了离线/在线功能,基于查尔斯代码:
<?php
Class AppController extends Controller{
// prevents unauthorized access
public $components = array('Auth');
// the name of the model storing site_offline boolean
public $uses = array('Configuration');
// callback invoked before every controller action
public function beforeFilter() {
// returns your site_offline status assuming 0 is offline
if ($this->Configuration->get_site_status() == 1) {
$this->Auth->allow('*');
}else {
if(($this->Configuration->get_site_status() == 0) and (!$this->Auth->user() == null)){
// I set it up like this for now to allow access to any authenticated user,
//but later will change it to only allow admins access thru a login form
$this->Auth->allow('*');
}else{
//If site is offline and user is not authenticated, sent them to
// the a screen using the OFFLINE layout and provide a screen for login.
$this->layout = 'offline';
$this->setFlash('Maintenance Mode. Check back shortly.');
$this->Auth->deny('*');
}
}
}
}
?>
然后我使用 jQuery 来隐藏我的登录表单。管理员单击消息以显示登录表单。这是为了防止任何登录尝试。
============================= 结束编辑================= ==========================
我想知道在 CakePHP 中创建“站点离线/在线”功能的最佳方式是什么。基本上,我想允许管理员关闭对每个注册或未注册的人的站点访问权限。离线页面应该有一个只有管理员才能登录的登录权限。
我的想法是创建某种仪表板控制器,管理员登录后,他/她将被重定向到此仪表板,从那里他可以访问其他控制器操作(admin_edit 等)。此仪表板和所有管理操作(admin_delete 等)应使用管理布局。
这是一个好方法吗?对于离线/在线功能,我应该创建一个带有可以打开或关闭的 site_offline 字段的设置表吗? app_controller 在哪里,在允许或不访问该站点之前我应该使用什么代码来检查它?
非常感谢您的帮助,
【问题讨论】:
-
您还可以查看 moving-a-cakephp-app 以及如何利用维护 shell 来激活/停用维护模式。
标签: cakephp