Thinkphp5.0快速入门笔记(1)
?
學(xué)習(xí)來(lái)源與說(shuō)明
https://www.kancloud.cn/thinkphp/thinkphp5_quickstart
測(cè)試與部署均在windows10下進(jìn)行學(xué)習(xí)。
Composer安裝和更新
Composer 是 PHP 用來(lái)管理依賴(dependency)關(guān)系的工具。可以在自己的項(xiàng)目中聲明所依賴的外部工具庫(kù)(libraries),Composer 會(huì)幫你安裝這些依賴的庫(kù)文件。
網(wǎng)址:https://www.phpcomposer.com/
下載:https://getcomposer.org/Composer-Setup.exe
ThinkPHP框架下載地址
[ Github ]
應(yīng)用項(xiàng)目: https://github.com/top-think/think
核心框架: https://github.com/top-think/framework
[ 碼云 ]
應(yīng)用項(xiàng)目: https://git.oschina.net/liu21st/thinkphp5.git
核心框架: https://git.oschina.net/liu21st/framework.git
[ Coding ]
應(yīng)用項(xiàng)目: https://git.coding.net/liu21st/thinkphp5.git
核心框架: https://git.coding.net/liu21st/framework.git
部署與測(cè)試
部署過(guò)程采用xampp搭建。
XAMPP(Apache+MySQL+PHP+PERL)是一個(gè)功能強(qiáng)大的建站集成軟件包。
下載完以后默認(rèn)安裝。安裝完畢后打開(kāi)xampp-control,如下界面。點(diǎn)擊Apache的start和MySQL的start,允許網(wǎng)絡(luò),輸入127.0.0.1測(cè)試。
Apache如果無(wú)法啟動(dòng)考慮端口占用問(wèn)題,使用端口80,443。在cmd窗口下命令:netstat -ano查看端口占用情況,然后在任務(wù)管理器中找到占用端口的對(duì)應(yīng)進(jìn)程PID,結(jié)束掉,重新啟動(dòng)Apache試試。
測(cè)試完畢后,在Apache的Config下的httpd.conf文件打開(kāi),在文本最后(568行最后左右)編輯輸入:
<VirtualHost *:80>DocumentRoot "C:\xampp\htdocs\server\public"ServerName www.server.com </VirtualHost>?然后在C:\xampp\htdocs下建立文件夾server,將php框架解壓到該文件夾下,檢查對(duì)應(yīng)出的public文件夾。
?然后在C:\Windows\System32\drivers\etc中,找到host文件,修改屬性,使可以編輯,然后在最后端添加:
192.168.0.103 www.matlabserver.com使可以通過(guò)域名對(duì)應(yīng)到網(wǎng)絡(luò)ip。其中的192.168.0.103為本機(jī)對(duì)應(yīng)在局域網(wǎng)的ip地址,用戶應(yīng)該在cmd窗口中使用ipconfig,自行查找對(duì)應(yīng)的局域網(wǎng)ip地址。
最后測(cè)試,瀏覽器中輸入ip,測(cè)試完畢后的效果如下:(當(dāng)然啦,現(xiàn)在thinkPHP都出到6.0版本了,如果使用了5.1版本或者6.0,應(yīng)該效果略有差異,但是不影響吧)
demo、控制器、視圖、調(diào)試開(kāi)關(guān)
demo
在ThinkPHP框架根路徑打開(kāi)cmd窗口,輸入
php think build --module demo會(huì)在application/下生成demo代碼作為示例。
控制器
控制器位于路徑application/index/controller/Index.php。編輯該文件即對(duì)主頁(yè)編輯。控制器的路徑和public/index.php配置有關(guān)。
修改application/index/controller/Index.php文件,則修改了主界面,如:
<?php namespace app\index\controller; class Index {public function index(){return 'Hello,World!';} }?
視圖
和demo一樣,在application/index/下創(chuàng)建view文件夾,如圖:
在之下創(chuàng)立hello.html,有如下內(nèi)容:
<html><head><title>hello {$name}</title></head><body>hello, {$name}!</body> </html>?修改application/index/controller/Index.php控制器為以下,則通過(guò)hello相互關(guān)聯(lián),控制器添加視圖文件功能。(使用use聲明繼承方便,不適用use則需要class Index extends \think\Controller聲明繼承。
<?php namespace app\index\controller; use think\Controller; class Index extends Controller {public function hello($name = 'thinkphp'){$this->assign('name', $name);return $this->fetch();} }?效果如下:(注意url訪問(wèn) http://serverName/index.php/模塊/控制器/操作)
調(diào)試開(kāi)關(guān)
調(diào)試開(kāi)關(guān)位于application/config.php,第20行左右修改為以下,則關(guān)閉了調(diào)試開(kāi)關(guān):
'app_debug' => false,打開(kāi)狀態(tài):
關(guān)閉狀態(tài):
與數(shù)據(jù)庫(kù)連接
?數(shù)據(jù)庫(kù)在xampp下的打開(kāi)方式,可以通過(guò)MySQL的admin打開(kāi),也可以在cmd窗口中操作。下圖為在xampp下的打開(kāi)mysql方式。
?
在cmd中操作,首先將環(huán)境變量添加到path中,然后再打開(kāi)cmd窗口。下圖為添加環(huán)境變量示例。
然后輸入如下,進(jìn)入mysql。
輸入以下,創(chuàng)建id,data的數(shù)據(jù)庫(kù),插入三條數(shù)據(jù)。
show databases;create database demo;use demoCREATE TABLE IF NOT EXISTS `think_data`( `id` int(8) unsigned NOT NULL AUTO_INCREMENT, `data` varchar(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;INSERT INTO `think_data`(`id`,`data`) VALUES (1,'thinkphp'), (2,'php'), (3,'framework');select * from think_data;?
一點(diǎn)效果:
?
在application/database.php中修改文件內(nèi)容為:
<?phpreturn [// 數(shù)據(jù)庫(kù)類型'type' => 'mysql',// 服務(wù)器地址'hostname' => '127.0.0.1',// 數(shù)據(jù)庫(kù)名'database' => 'demo',// 數(shù)據(jù)庫(kù)用戶名'username' => 'root',// 數(shù)據(jù)庫(kù)密碼'password' => '',// 數(shù)據(jù)庫(kù)連接端口'hostport' => '',// 數(shù)據(jù)庫(kù)連接參數(shù)'params' => [],// 數(shù)據(jù)庫(kù)編碼默認(rèn)采用utf8'charset' => 'utf8',// 數(shù)據(jù)庫(kù)表前綴'prefix' => 'think_',// 數(shù)據(jù)庫(kù)調(diào)試模式'debug' => true,];?修改控制器代碼為:
<?php namespace app\index\controller; use think\Controller; use think\Db; class Index extends Controller {public function index(){$data = Db::name('data')->find();$this->assign('result', $data);return $this->fetch();} }?添加模板文件view/index.html,設(shè)立內(nèi)容為:
<html><head><title></title></head><body>{$result.id}--{$result.data}</body> </html>?保存,最終效果為:
?成功連接上了數(shù)據(jù)庫(kù),然后輸出第一條數(shù)據(jù)。
?也可以嘗試更改控制器代碼第九行為:
$data = Db::name('data')->where('id',2)->find();?輸出數(shù)據(jù)庫(kù)第二條數(shù)據(jù)。
參閱地址:https://www.kancloud.cn/manual/thinkphp5/135176
?
轉(zhuǎn)載于:https://www.cnblogs.com/bai2018/p/11291674.html
總結(jié)
以上是生活随笔為你收集整理的Thinkphp5.0快速入门笔记(1)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 迁移数据时 timestamp类型字段报
- 下一篇: 动态规划算法php,php算法学习之动态