mosakabe @ ウィキ
CakeSample
最終更新:
mosakabe
-
view
models/TestTable.php
<?php class TestTable extends AppModel{ } ?>
controllers/hello_world_controller.php
<?php class HelloWorldController extends AppController { public $uses=array('TestTable'); public function index(){ $this->log('output default log.'); $this->log('output error log.', LOG_ERROR); $this->log('output debug log.', LOG_DEBUG); CakeLog::write(LOG_ERROR, 'output error CakeLog.'); CakeLog::write(LOG_DEBUG, 'output debug CakeLog.'); CakeLog::debug('output debug() CakeLog.'); CakeLog::info('output info() CakeLog.'); CakeLog::error('output error() CakeLog.'); $keyword=empty($this->params['data']['TestTable']['keyword'])?'':$this->params['data']['TestTable']['keyword']; $this->paginate=array('conditions'=>array('or'=>array('TestTable.name LIKE'=>"%{$keyword}%"))); $this->set('list',$this->paginate('TestTable')); $this->set('keyword',$keyword); } } ?>
views/hello_world/index.ctp
<?php echo $this->Form->create(); echo $this->Form->input('keyword',array('type'=>'text','value'=>$keyword)); echo $this->Form->submit(); echo $this->Form->end(); echo $paginator->options(array('url'=>array('keyword'=>urlencode($keyword)))); echo $paginator->prev('前へ'); echo ' ' . $paginator->numbers() . ' '; echo $paginator->next('次へ'); echo $html->tag('table'); echo $html->tableHeaders(array('ID','名称','日時')); foreach( $list as $data ){ $d = $data['TestTable']; echo $html->tableCells(array($d['test_table_id'],$d['name'],$d['timestamp'])); } echo $html->tag('/table'); ?>
app/webroot/index.php
28 require_once( dirname( dirname( __FILE__ ) ) . DS . 'app_log.php' );
app/app_log.php
<?php // app/app_log.php class CakeLog{ const DEBUG = 'DEBUG'; const INFO = 'INFO_'; const ACCESS = 'ACCES'; const ERROR = 'ERROR'; const EXCLUSION = 'object.php|debugger.php'; function CakeLog(){ $args = func_get_args(); if( method_exists( $this, '__destruct' ) ){ register_shutdown_function( array( &$this, '__destruct' ) ); } } function __destruct() { } function write( $type, $msg ){ if( empty( $type ) || $type == LOG_DEBUG ){ $label = self::DEBUG; }else{ $label = self::ERROR; } self::__force( $label, strip_tags( $msg ) ); } function debug( $msg ){ self::__force( self::DEBUG, $msg ); } function info( $msg ){ self::__force( self::INFO, $msg ); } function access( $msg ){ self::__force( self::ACCESS, $msg ); } function error( $msg ){ self::__force( self::ERROR, $msg ); } private static function __force( $label, $msg ){ $msg = ( is_string( $msg ) || ( $msg instanceof Exception ) ) ? $msg : var_export( $msg, true ); $bt = debug_backtrace(); if( isset( $bt[1]['file'] ) && !ereg( self::EXCLUSION, $bt[1]['file'] ) ){ $depth = 1; }else if( isset( $bt[2]['file'] ) && !ereg( self::EXCLUSION, $bt[2]['file'] ) ){ $depth = 2; }else{ $depth = 3; } $called = $bt[$depth]; $date = date( 'Y/m/d H:i:s' ) . substr( microtime( true ) . '000', 10, 4 ); $ip = $_SERVER["REMOTE_ADDR"]; $account = 'nologin'; $file = str_replace( APP, '', $called['file'] ); $line = $called['line']; $head = $date . ' ' . $label . ' ' . $ip . ' ' . $account . ' ' . $file . ':' . $line; error_log( $head . "\n" . $msg . "\n" , 3, '/var/log/cake/expand/' . date( 'Ymd' ) . '.log' ); $msg = str_replace( "\n", '_LF_', str_replace( "\r", '_CR_', $msg ) ); error_log( $head . "\t" . $msg . "\n" , 3, '/var/log/cake/inline/' . date( 'Ymd' ) . '.log' ); } } class_alias('CakeLog', 'Log');
app/controllers/app_controller.php
<?php class AppController extends Controller { public function __construct() { parent::__construct(); set_exception_handler( array( $this, 'exception_handler' ) ); } function beforeFilter(){ parent::beforeFilter(); Log::access('アクセス'); $this->cert(); } function cert(){ Log::info('認証'); } function exception_handler( $e ) { Log::error( $e ); $exception_class_name = get_class( $e ); switch( $exception_class_name ){ case 'QueryExecuteException': $this->redirect('/error/db_error'); break; default: $this->cakeError('error404'); break; } } }