phpcms有控制器。phpcms控制器是模塊的類文件,儲(chǔ)存在“phpcms/modules”下面,每個(gè)模塊都是獨(dú)立的命名方式,類名是“文件名+.php命名”格式;控制器類默認(rèn)繼承系統(tǒng)的函數(shù)庫(kù),可以直接使用??刂破黝惖念惷Q與控制器文件名必須相同。
本教程操作環(huán)境:windows7系統(tǒng)、phpcms v9版,DELL G3電腦
phpcms有控制器。
phpcms中什么是控制器
phpcms控制器是模塊的類文件,儲(chǔ)存在phpcms/modules下面,每個(gè)模塊都是獨(dú)立的命名方式,類名是文件名+.php命名格式,控制器類默認(rèn)繼承系統(tǒng)的函數(shù)庫(kù),可以直接使用??刂破黝惖念惷Q與控制器文件名必須相同。
新增一個(gè)控制器
下面我們就在phpcms/modules下面新建一個(gè)test文件夾,在test文件夾里面新建一個(gè)名叫mytest.php的文件,在文件里面添加以下代碼:
defined('IN_PHPCMS') or exit('No permission resources.');class mytest { function __construct() { } public function init() { $myvar = '這是默認(rèn)加載!'; echo $myvar; } public function mylist() { $myvar = '這是自定義list!'; echo $myvar; } }
然后我們?cè)跒g覽器輸入以下訪問(wèn)方式即可
http://域名/index.php?m=test&c=mytest
默認(rèn)加載init()方法
http://域名/index.php?m=test&c=mytest&a=mylist
加載的是mylist方法
模板介紹
前臺(tái)模板
網(wǎng)站前臺(tái)模板位置在phpcms/templates/default/模塊下面
我們?cè)诳刂破鱩ytest.php文件init()方法里面加入加載模板方法
include template('test', 'mytest', 'default');
在phpcms/templates/default/目錄下面新建一個(gè)test文件夾和mytest.html文件即可。
后臺(tái)模板
后臺(tái)模板文件在phpcms/modules/模塊名稱/templates 目錄中
帶權(quán)限的控制器
后臺(tái)控制器
在phpcms/modules/admin/下面新建一個(gè)mytest_admin.php文件,文件代碼如下:
defined('IN_PHPCMS') or exit('No permission resources.'); pc_base::load_app_class('admin','admin',0); class mytest_admin extends admin{ function __construct() { } public function index(){ echo "后臺(tái)控制器"; } }
訪問(wèn)地址:http://域名/index.php?m=admin&c=mytest_admin&a=index
phpcms自定義模型
在類上面引入模型
pc_base::load_sys_class('model', '', 0);
然后類extends繼承model
在__construct方法里面添加以下代碼:
$this->db_config = pc_base::load_config('database'); $this->db_setting = 'default'; parent::__construct();
查詢語(yǔ)句
function init(){ $sql = 'select * from v9_news '; $data = $this->get_array_by_sql($sql); var_dump($data); } public function sql_query($sql) { if (!empty($this->db_tablepre)) $sql = str_replace('phpcms_', $this->db_tablepre, $sql); return parent::query($sql); } public function fetch_next() { return $this->db->fetch_next(); } //通過(guò)SQL語(yǔ)句查詢一條結(jié)果 public function get_one_by_sql($sql){ $this->sql_query($sql); $res = $this->fetch_next(); $this->free_result(); return $res; } //通過(guò)sql語(yǔ)句查詢數(shù)組 public function get_array_by_sql($sql){ $this->sql_query($sql); $res = $this->fetch_array(); $this->free_result(); return $res; } //釋放數(shù)據(jù)庫(kù)結(jié)果資源,調(diào)用底層完成 public function free_result() { $this->db->free_result(); }
PHP中文網(wǎng),大量的免費(fèi)PHPCMS教程,歡迎在線學(xué)習(xí)!