【问题标题】:CakePHP - Create an Offline/Online feature for a siteCakePHP - 为网站创建离线/在线功能
【发布时间】: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


【解决方案1】:

首先在core.config

中添加一个配置
/*
 * This is the site maintenance 
 *   The built in defaults are:
 *
 * - '1' - Site works
 * - '0' - site down for maintenance.
 */

Configure::write('Site.status', 1);

AppController 中,您将在 beforeRender 函数中检查它

if (Configure::read('Site.status') == 0) {
            $this->layout = 'maintenance';
            $this->set('title_for_layout', __('Site_down_for_maintenance_title'));
        } else {
// do something
}

我在这里从维护中加载一个单独的布局,让我添加我想要的任何布局

【讨论】:

  • 我前一阵子处理了这个问题,但使用的是数据库而不是 core.php 文件。通过使用数据库,我可以在管理部分轻松打开和关闭站点
  • 您可能想要beforeFilter 而不是beforeRender,因为后者在 一个动作之后被调用并且可以从数据库中读/写(大概你不'不想在维护模式下)。
【解决方案2】:

如果您打算将 site_offline 布尔值保存在数据库表中,您应该能够通过 AppController 和 Auth 组件中的回调轻松地做到这一点。

<?php

AppController extends Object {

    // prevents unauthorized access
    public $components = array('Auth');

    // the name of the model storing site_offline boolean
    public $uses = array('NameOfModel');

    // callback invoked before every controller action
    public function beforeFilter() {

        // returns your site_offline status assuming 0 is offline
        if ($this->NameOfModel->get_status() === 0) {
             $this->Auth->deny('*');
        } else {
             $this->Auth->allow('*');
        }

    }

}

我一直很喜欢 DashboardsController 用于管理功能的想法。这实际上是我使用的类的确切名称和相同的总体思路。

【讨论】:

  • 感谢您的建议。这看起来很合法。我会尝试并回帖。
  • Charles,我已根据您的帮助使用解决方案更新了我的原始问题。再次感谢!
猜你喜欢
  • 2016-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多