1. CakePHP的应用程序脚手架
到CakePHP的app目录下
eg:
cd ../CakePHP/app
php ..\cake\console\cake.php bake
按提示步骤操作
Model
Controler
View
...
参考资料:
http://book.cakephp.org/cn/view/1522/Code-Generation-with-Bake
2. ACL 访问控制
参考《cakephp acl 使用说明》
参考资料:
http://book.cakephp.org/cn/view/1543/Simple-Acl-controlled-Application ACL应用的简单示例
3. CAS的集成
* 资源准备
# CAS的php客户端
下载地址: http://downloads.jasig.org/cas-clients/php/current/CAS-1.2.0.tgz
当前版本: 1.2.0
# CakePHP引入CAS客户端
把CAS客户端复制到CakePHP的./app/venders下
# 在项目中应用CAS客户端
在./app/app_controller.php里面引入CAS.php
eg: App::import('Vendor', 'phpCAS', array('file' => 'CAS.php'));
代码
eg:// 初始化 phpCAS hostname 域名 port 端口 appname 应用名称
phpCAS::client(CAS_VERSION_2_0, $hostname, $port, $appname);
// no SSL validation for the CAS server
phpCAS::setNoCasServerValidation();
// force CAS authentication
phpCAS::forceAuthentication();
// at this step, the user has been authenticated by the CAS server
// and the user's login name can be read with phpCAS::getUser().
// logout if desired
if (isset($_REQUEST['logout'])) {
phpCAS::logout();
}
//获得登录的用户名
$this->loggedUser = phpCAS::getUser();
* 参考资料
https://wiki.jasig.org/display/CASC/phpCAS
4. JIRA接口的使用
* jira配置要求
参照Url: http://confluence.atlassian.com/display/JIRA/Creating+a+SOAP+Client
* 权限Check
a. does the intended developer have the permission to be assigned issues?
b. does the Jira Administrator has the permission to assign issues? (remember, a Jira Admin can administrate the system, it does not necessarily grant any rights in projects...)
c.I'm not so sure about this one, but I think for SOAP to set a field, you might have to have the field on the "Create" screen.
Simple way to test this - log in as the Jira Admin user and click "create" and check what appears in the "assignee" field...
* create issue
在创建页面需显示创建issue时必要的field。
备注:如果不显示assignee的选项,在soap中不能修改assignee,显示为默认项目负责人。同时创建用户需要assgin权限。
代码:
// Jira WSDL
$wsdl = “http://localhost:8080/rpc/soap/jirasoapservice-v2?wsdl”;
// Login info
$login = “admin”;
$password = “111111”;
// Create the soap Client
$client = new soapclient($wsdl);
// Login to Jira
$login = $client->login( $login,$password);
$project = "CHECKING";
$type = 6;
$date = date('Ymd');
$detailUrl = JIRA_DETAIL_URL;
$userName = “test”;
$remoteIssue = array(array ("customfieldId"=>"customfield_10050", "values"=>array (“test0”)),
array ("customfieldId"=>"customfield_10123", "values"=>array (“test1”)),
array ("customfieldId"=>"customfield_10167", "values"=>array (“test2”)));
$issue = array(
"project" => $project,
"type" =>3,
"summary" => “TESTDEMO”,
"assignee"=>$userName,
"reporter"=>$login,
"customFieldValues" => $remoteIssue
);
// Create the Issue
$remoteIssue = $client->createIssue( $login,$issue);
// Add p_w_upload
$p_w_upload_file =”d:\\1.txt”;
$content = base64_encode(file_get_contents($p_w_upload_file));
$p_w_uploadName = basename($p_w_upload_file);
$result = $client->addBase64EncodedAttachmentsToIssue($login, $remoteIssue->key, array($p_w_uploadName), array($content) );
?>
5. 自定义Component(组件)
a. 日志组件LogHelper
参考《CakePHP 日志组件LogHelper》
b. 防止重复提交组件TokenHelper
为了防止form重复提交,而选择的token控件!
使用说明:
# 定义token控件 token_helper.php
class TokenHelperComponent extends Object {
var $components = array('Session');
var $token = null;
//called before Controller::beforeFilter()
function initialize(&$controller, $settings = array()) {
// saving the controller reference for later use
$this->controller =& $controller;
}
function token(){
$this->token = mt_rand(0,100000000);
$this->Session->write('token',$this->token);
}
function getToken(){
return $this->token;
}
function isFirstSubmit( $token = null ){
if($this->Session->read('token') == $token){
return true;
}
return false;
}
function del(){
$this->Session->write('token','');
$this->token = null;
return;
}
//called after Controller::render()
function shutdown(&$controller) {
}
}
?>
# controller中的使用
* 引入控件
var $components = array('TokenHelper');
* form入口method中使用
$this->TokenHelper->token();
......
$this->set('token', $this->TokenHelper->getToken());
* form提交method中使用
if($this->TokenHelper->isFirstSubmit($this->data['token'])){
$this->TokenHelper->del();
......
}else{
//重复提交的结果
......
}
# form页面
* 引入token
Form->input('token',array('label'=>false,'div'=>false,'class'=>'from_input','type'=>'hidden','value'=>$token)); ?>
备注:
参考php重复提交处理方案
c. 文件操作组件FileHelper
文件操作控件集合创建目录、文件,读取文件、文件内容、文件扩展名,删除文件等操作!
# cakephp扩展组件
php代码[app/controllers/components目录下]
file_helper.php
代码:
class FileHelperComponent extends Object {
var $components = array('LogHelper');
//called before Controller::beforeFilter()
function initialize(&$controller, $settings = array()) {
// saving the controller reference for later use
$this->controller =& $controller;
}
/**
* create dir
*/
function mkFiledir($path){
if(!file_exists($path)){
mkdir($path);
}
}
/**
* create file
*/
function createFile($filename,$content = null){
$ourFileName = $filename;
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fwrite($ourFileHandle, $content);
fclose($ourFileHandle);
}
/**
* append content
*/
function appendFile($filename,$appendFile = null){
$ourFileName = $filename;
$content = '';
if (file_exists($appendFile) && is_file($appendFile)) {
$file = fopen($appendFile, "r") or exit("Unable to open file!");
//Output a line of the file until the end is reached
while(!feof($file)){
$line = fgets($file);
$content .= $line.PHP_EOL;
}
}else{
$this->LogHelper->setAddLog(array("SMessage"=>$rCurShaFileName."文件找不到","Result"=>false));
return;
}
if(!empty($content)){
$content = PHP_EOL.$content;
}
$ourFileHandle = fopen($ourFileName, 'a+') or die("can't open file");
fwrite($ourFileHandle, $content);
fclose($ourFileHandle);
}
/**
* exist file
*/
function isFileExist($filename){
if (file_exists($filename) && is_file($filename)) {
return true;
}
return false;
}
/**
* delete file
*/
function delFile($filename){
if (file_exists($filename) && is_file($filename)) {
unlink($filename);
}
}
/**
* file extension
*/
function extend($file_name){
$extend = pathinfo($file_name);
$extend = strtolower($extend["extension"]);
return $extend;
}
/**
* file content
*/
function getContent($filename){
$content = file_get_contents($filename);
return $content;
}
/**
* base name
*/
function getBaseName($filename){
$name = basename($filename);
return $name;
}
function download($fileName, $downloadFile) {
$speed = 100; // 8,5 kb/s download rate limit
if (file_exists($downloadFile) && is_file($downloadFile)) {
header('Cache-control: private');
header('Content-Type: application/octet-stream');
header('Content-Length: ' . filesize($downloadFile));
header('Content-Disposition: filename=' . $fileName);
flush();
$fd = fopen($downloadFile, 'r');
while (!feof($fd)) {
echo fread($fd, round($speed * 1024));
flush();
sleep(1);
}
fclose($fd);
$this->LogHelper->setLog('download',array("File"=>$downloadFile,"Result"=>true));
} else {
$this->LogHelper->setLog('download',array("File"=>$downloadFile,"Message"=>"文件不存在!","Result"=>false));
return;
}
}
//called after Controller::render()
function shutdown(&$controller) {
}
}
?>
# controller中的使用
* 引入控件
var $components = array('FileHelper');
* method中使用
eg:
//创建目录
$this->FileHelper->mkFiledir($path);
//创建文件
$this->FileHelper->createFile($filename, $content);
//在file1上追加file2文件内容
$this->FileHelper->appendFile($file1, $file2);
//判断文件是否存在
$this->FileHelper->isFileExist($filename);
//删除文件
$this->FileHelper->delFile($filename);
//获得文件扩展名
$this->FileHelper->extend($filename);
//获得文件内容
$this->FileHelper->getContent($filename);
//获得文件名
$this->FileHelper->getBaseName($filename);
d. 时间操作组件DateHelper
日期时间的操作方法
# cakephp扩展组件
php代码[app/controllers/components目录下]
date_helper.php
代码:
class DateHelperComponent extends Object {
//called before Controller::beforeFilter()
function initialize(&$controller, $settings = array()) {
// saving the controller reference for later use
$this->controller =& $controller;
}
/**
* java时间毫秒数转php时间
* format "Y-m-d H:i:s"
*/
function javaTimeMilToPhpDate($format, $time){
return date($format,$time/1000);;
}
/**
* format Y-m-d
*/
function dates_inbetween($date1, $date2, $format){
$day = 60*60*24;
$date1 = strtotime($date1);
$date2 = strtotime($date2);
$days_diff = round(($date2 - $date1)/$day); // Unix time difference devided by 1 day to get total days in between
$dates_array = array();
$dates_array[] = date($format,$date1);
for($x = 1; $x < $days_diff; $x++){
$dates_array[] = date($format,($date1+($day*$x)));
}
$dates_array[] = date($format,$date2);
return array_unique($dates_array);
}
/**
* date compare
*/
function greaterDate($start_date,$end_date){
$start = strtotime($start_date);
$end = strtotime($end_date);
if ($start-$end > 0)
return 1;
else
return 0;
}
//called after Controller::render()
function shutdown(&$controller) {
}
}
?>
# controller中的使用
* 引入控件
var $components = array('DateHelper');
* method中使用
eg:
//java时间毫秒数转成php的Date数据
$this->DateHelper->javaTimeMilToPhpDate($format, $time);
//两个日期间的日期列表
$this->DateHelper->dates_inbetween($date1, $date2, $format);
//两个日期间的比较
$this->DateHelper->greaterDate($date1, $date2);
e. Redis的客户端Predis应用组件RedisClient
redis的操作方法
# cakephp扩展组件
php代码[app/controllers/components目录下]
redis_client.php
代码:
App::import('Vendor', 'Predis_Client', array('file' =>'Predis'.DS.'SharedConfigurations.php'));
class RedisClientComponent extends Object {
// Predis info
var $redis = null;
var $redis_publish = null;
var $pubsub = null;
//called before Controller::beforeFilter()
function initialize(&$controller, $settings = array()) {
// saving the controller reference for later use
$this->controller =& $controller;
//redis
$this->_initCommonClient();
}
function _initCommonClient(){
$this->redis = new Predis_Client(array(
'host' => REDIS_HOST,
'port' => REDIS_PORT,
));
}
/**
* client add value
*/
function setValue( $key, $val ){
//不存在就添加
if(!$this->redis->exists($key)){
$this->redis->set($key, $val);
}else{
$this->redis->del($key);
$this->redis->set($key, $val);
}
}
/**
* client get value
*/
function getValue($key){
return $this->redis->get($key);
}
/**
* client delete value
*/
function del($key){
if($this->redis->exists($key)){
$this->redis->del($key);
}
}
//called after Controller::render()
function shutdown(&$controller) {
}
}
?>
# controller中的使用
* 引入控件
var $components = array('RedisClient');
* method中使用
eg:
//添加值
$this->RedisClient->setValue( $key, $val );
//获得值
$this->RedisClient->getValue($key);
//删除值
$this->RedisClient->del(key);