Loadsys

LoadStr - A Loadsys Blog

A Web Development Company Specializing In Standard-Based Web Design and the CakePHP Framework.

Posts Tagged ‘CakePHP’

Automagically setting user ID of record creator and modifier in CakePHP 1.2

Friday, May 2nd, 2008

When designing a system for multiuser environment, it is always important to know who created and who modified a record. When using proper field names, such as created and modified, CakePHP automatically sets the proper dates values. We wanted to use the same principal for automatically setting user ID of who created and modified a record. We created a standard for ourself by naming the fields creator_id and modifier_id which goes together with Cake’s date fields created and modified.

All of our projects use as much of standard CakePHP libraries as possible. That’s why all of our CakePHP projects use AuthComponent for authentication.

The first thing we needed to do to was to create ability to access ID of a currently logged in user anywhere in the application. This was achieved by overloading the core AuthComponent:

controllers/components/loadsys_auth.php

<?php

uses('controller/components/auth');

class LoadsysAuthComponent extends AuthComponent {

        function initialize(&$controller) {
                ClassRegistry::addObject('LoadsysAuthComponent', $this);
                parent::initialize($controller);
        }

}

class LoadsysAuth {

        function &getInstance() {
                static $instance = array();
                if (!$instance) {
                        $instance[0] =& ClassRegistry::getObject('LoadsysAuthComponent');
                }
                return $instance[0];
        }

        static function getUser() {
                $_this =& LoadsysAuth::getInstance();
                return $_this->user();
        }

        static function getUserId() {
                $_this =& LoadsysAuth::getInstance();
                $user = $_this->user();
                return Set::extract($user, 'User.id');
        }

        static function getUserGroupId() {
                $_this =& LoadsysAuth::getInstance();
                $user = $_this->user();
                return Set::extract($user, 'User.user_group_id');
        }
}
?>

This structure will allow us to access user ID and user group ID from anywhere in the application by calling LoadsysAuth::getUserId() and LoadsysAuth::getUserGroupId(). This could be applied to many situations.

Now we need to include the component in our project:

app_controller.php

class AppController extends Controller {
	...
	var $components = array('LoadsysAuth');

	...
}

In order to set the creator_id and modifier_id for, we will have to put it in the base class:

app_model.php

<?php

class AppModel extends Model {
        function beforeSave() {
                $exists = $this->exists();
                if ( !$exists && $this->hasField('creator_id') && empty($this->data[$this->alias]['creator_id']) ) {
                        $this->data[$this->alias]['creator_id'] = LoadsysAuth::getUserId();
                }
                if ( $this->hasField('modifier_id') && empty($this->data[$this->alias]['modifier_id']) ) {
                        $this->data[$this->alias]['modifier_id'] = LoadsysAuth::getUserId();
                }
                return true;
        }
}

?>

In your application models, if you need to override the beforeSave callback, make sure you call the parent function:

Example:

create table articles (
 id int unsigned not null auto_increment,
 title varchar(255) not null,
 article varchar(255) not null,
 created datetime,
 creator_id int unsigned,
 modified datetime,
 modifier_id int unsigned,
 primary key(id)
);

models/article.php

<?php

class Article extends AppModel {
	...

	function beforeSave() {
		...
		return parent::beforeSave();
	}

	...
}

?>

SEO for CakePHP Websites

Monday, March 10th, 2008

There is little web developers can do to get high rankings in search engines because most rankings are because of backlinks to your site, however we can better the possibility by utilizing fundamental rules that make more pages of your website indexed.

We develop in a framework called CakePHP.  It is getting very popular in the web development world.  However, CakePHP is simply what it is set out to be which is a framework and not a CMS or anything of that nature.  It is a framework that gives you a structured way to develop web applications and gives you handy tools to speed up the process.

Because of this, there is of course no SEO features built into its core.  However, with the number of SEO companies that are pushing our clients to implementing very important “SEO-Friendly” items, we have set out to make this a feature that is with every CakePHP application we develop.
(more…)

Close
E-mail It