Coder Social home page Coder Social logo

active-admin-cakephp's Introduction

Active Admin for CakePHP 2.x -- 2.0/2.1 compliance may need some tweaks

Based on Active Admin for RoR (http://activeadmin.info/). This plugin for CakePHP gives you the same administration interface for the PHP framework. It also uses Nik Chankov's Filter component (http://nik.chankov.net). This install assumes that you've setup your prefix to be admin using the following routing prefix:

Configure::write('Routing.prefixes', array('admin'));

Essentially this will create the backend at a url like: http://your-domain-here.com/admin

Install

1 - Clone the project as "ActiveAdmin" into your apps plugins-folder (app/Plugin/)

2 - Enable the plugin in your app/Config/bootstrap.php file

CakePlugin::load(array('ActiveAdmin' => array('routes' => true)));

3 - Open (or create) your app/Controller/AppController.php file and add the following:

/**
 * Before Filter method
 *
 * @return void
 */
function beforeFilter() {
    if (isset($this->params['prefix']) && $this->params['prefix'] == 'admin') {
        $this->layout = 'ActiveAdmin.admin';
        // Auth is used here and checked for a valid user
        if ($user = $this->Auth->user()){
            if(!$this->isAuthorized($user)){
                $this->redirect($this->Auth->logout());
            }
        }
    }else{
        $this->Auth->allow('*');
    }
}

/**
 * User Auth check method
 *
 * @return void
 */    
public function isAuthorized($user) {
    // Admin can access every action
    if (isset($user['role']) && $user['role'] === 'admin') {
        return true;
    }

    // Default deny
    return false;
}

As you can see above, ActiveAdmin uses the user login functionality, and this will require the Auth Component to be enabled in your AppController:

var $components = array('Auth');

Prepare your app's controllers

4 - The Filter component is needed for filtering of records - This can be added on a per controller basis or simply added to the app/Controller/AppController.php file for all controllers to use

var $components = array('ActiveAdmin.Filter');

For filters to work in all plugins, modify the first line of your AppController to have:

App::uses('Controller', 'Controller');
App::uses('File', 'Utility');
App::uses('Folder', 'Utility');

5 - For the admin_index function:

function admin_index() {
    $this->Post->recursive = 0;
    // Add this 
    $filter = $this->Filter->process($this);
    $this->set('posts', $this->paginate(null, $filter));
}

6 - And update your View/(Controller)/admin_index.ctp views, using a table-header element that enable table-sorting:

<table cellpadding="0" cellspacing="0">
<?php echo $this->element('table_header', array('keys'=>array('id', 'title', 'label' => 'slug','created', 'modified')), array('plugin'=>'ActiveAdmin')); ?>
  <?php
  $i = 0;
  foreach ($posts as $post):
    $class = null;
    if ($i++ % 2 == 0) {
      $class = ' class="even"';
    }
  ?>
  <tr<?php echo $class;?>>
    <td><?php echo $post['Post']['id']; ?>&nbsp;</td>
    <td><?php echo $post['Post']['title']; ?>&nbsp;</td>
    <td><?php echo $post['Post']['slug']; ?>&nbsp;</td>
    <td><?php echo $post['Post']['created']; ?>&nbsp;</td>
    <td><?php echo $post['Post']['modified']; ?>&nbsp;</td>
    <td class="actions">
      <?php echo $this->Html->link(__('View'), array('action' => 'view', $post['Post']['id'])); ?>
      <?php echo $this->Html->link(__('Edit'), array('action' => 'edit', $post['Post']['id'])); ?>
      <?php echo $this->Html->link(__('Delete'), array('action' => 'delete', $post['Post']['id']), null, __('Are you sure you want to delete # %s?', $post['Post']['id'])); ?>
    </td>
  </tr>
<?php endforeach; ?>
</table>

7 - Create the table for ActiveAdmin using the schema shell:

./Console/cake schema create --plugin ActiveAdmin --name dashboard

8 - Adding Admin Menu controller items can be done via the provided console shell (eg. adding Posts or the Categories from Blog plugin)

./Console/cake ActiveAdmin.resource Posts
./Console/cake ActiveAdmin.resource Blog.Categories

If you're experiencing some issues with the filter, make sure that the display field is set in your model:

var $displayField = "title";

The above would set the filter search on the title field of the model.

active-admin-cakephp's People

Contributors

gerhardsletten avatar tecknix avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.