本篇,咱們的目標有三個:php
原文地址:http://www.voidking.com/2017/...mysql
vkphp ├─app │ ├─ctrl │ ├─model │ ├─smarty │ └─view ├─config ├─core ├─lib ├─log └─public
咱們以前的路由配置、數據庫配置都是寫在程序中的,修改起來不方便,下面咱們把這些配置都放在配置文件中。git
在core目錄中,新建conf.php,內容以下:github
<?php namespace core; class conf{ public function get($name,$file){ /** * 一、判斷文件是否存在 * 二、判斷配置是否存在 */ $file_path = CONFIG.'/'.$file.'.php'; if(is_file($file_path)){ $conf = include $file_path; if(isset($conf[$name])){ return $conf[$name]; }else{ throw new \Exception('沒有配置項'.$name); } }else{ throw new \Exception('找不到配置文件'.$file); } } public function all($file){ $file_path = CONFIG.'/'.$file.'.php'; if(is_file($file_path)){ $conf = include $file_path; return $conf; }else{ throw new \Exception('找不到配置文件'.$file); } } }
在config目錄中,新建配置文件route_config.php和db_config.php。sql
<?php return array( 'CTRL'=>'index', 'ACTION'=>'index' );
<?php return array( 'DSN'=>'mysql:host=localhost;dbname=vkphp', 'USER'=>'root', 'PASSWD'=>'' );
修改core目錄中的route.php和db.php。數據庫
<?php /** * 路由控制 */ namespace core; class route{ public $ctrl='index'; public $action='index'; public $params=array(); public function __construct(){ //echo 'route is ready!'; /** * 一、隱藏index.php * 二、獲取URL中的控制器和方法 * 三、獲取URL中的參數 */ $this->ctrl = \core\conf::get('CTRL','route_config'); $this->action = \core\conf::get('ACTION','route_config'); // 其餘不變 }
<?php namespace core; class db extends \PDO{ public function __construct(){ $conf = \core\conf::all('db_config'); $dsn = $conf['DSN']; $user = $conf['USER']; $passwd = $conf['PASSWD']; try{ parent::__construct($dsn,$user,$passwd); // echo 'database connect success'; }catch (\Exception $e){ echo $e->getMessage(); } } }
在core目錄,添加log.php文件,內容以下:json
<?php namespace core; class log{ public function log($message,$file_name){ $log_path = LOG.'/'.$file_name.'-'.date('YmdHis').'.log'; $message = date('Y-m-d H:i:s').' '.$message; file_put_contents($log_path,json_encode($message)); } }
在indexCtrl.php中,添加:app
public function log(){ $log = new \core\log(); $log->log('this is log','log_test'); echo '成功寫入日誌'; }
訪問 http://vkphp.dev/index/log ,便可在log目錄下生成日誌文件。
若是時間不正確,就在php.ini中搜索 date.timezone
,而後修改時區爲:框架
data.timezone="Asia/Shanghai"
或者,直接在程序代碼中使用函數ini_set('date.timezone','Asia/Shanghai');
,或者date_default_timezone_set(‘Asia/Shanghai');
。函數
https://github.com/voidking/v...