thinkphp 學習要點記錄

thinkphp6.0 學習要點記錄

技術時間都有限,自己比較懶,就不學習比較深入的了,夠自己用就行,介意的不要噴我,繞道即可,針對6.0版的,版本不一致的不要較真,謝謝

關於路由

可以不配置,直接通過controller中的類名/方法名訪問就可以訪問到,

關於數據庫查詢的快捷寫法

  public function test1()
    {
        $res = Db::table('user')
            ->field('user_id,name,pwd')
            ->where('user_id>=1 and user_id<=2')
            ->order('user_id desc, name asc')
            ->select();
        return $res;
    }

關於數據庫配置(再.env文件中)

APP_DEBUG = true

[APP]
DEFAULT_TIMEZONE = Asia/Shanghai

[DATABASE]
TYPE = mysql
HOSTNAME = 127.0.0.1
DATABASE = tp
USERNAME = tp
PASSWORD = *****
HOSTPORT = 3306
CHARSET = utf8
DEBUG = true

[LANG]
default_lang = zh-cn

關於安裝

composer create-project topthink/think tp

modle寫法

class User extends Model
{
    protected $table = 'user';
    protected $pk = 'user_id';
}

數據庫操作

  public function c(UserModel $userModel)
    {
        $res = $userModel->db()
            ->where('user_id','<',2)
            ->whereOr('user_id','>=',3)
            ->select();
        return $res;
    }

    public function create()
    {
        $data = ['name' => '102', 'pwd' => 'password(1234)', 'role' => 0];
        $userModel = UserModel::create($data);
        return $userModel['name'];
    }

    public function update()
    {
       $userModel= UserModel::update(['c1'=>1],['user_id'=>'2']);
       return $userModel['name'];
    }
官方實例:
// 根據主鍵刪除
Db::table('think_user')->delete(1);
Db::table('think_user')->delete([1,2,3]);

// 條件刪除    
Db::table('think_user')->where('id',1)->delete();
Db::table('think_user')->where('id','<',10)->delete();

更多官方例子看這裏:https://www.kancloud.cn/manual/thinkphp6_0/1037535

數組添加

<?php
$a=array("red","green");
array_push($a,"blue","yellow");
print_r($a);
?>

模板流程控制(原生和模板語言)

遍歷

//原生寫法
<?php foreach($users as $key =>$user:)?>
	<?php echo $key; ?>
	<?php echo $user['name'] ;?>
	<?php echo $user['id']; ?>
<?php endforeach; ?>
//原生簡寫
<?php foreach($users as $key=$user:);?>
	<p><?$key?>   <?$user['name']?>  <?$user['id']?></p>
<?php endforeach;?>
//模板語言
{foreach $users as $key=>$user}
<p>{$key}--{$user['name']}--{$user['id']}</p>
{/foreach}
//模板語言2
<volist name='users' id='user'>
//條件控制
{if($user['id']>10)}
<p>{$key}--{$user['name']}--{$user['id']}</p>
</volist>
//模板語言3

首頁訪問正常,其他接口訪問404

試着在域名後加index.php然後再加接口其他內容訪問,即可訪問
如:http://a.a.com/index/a訪問顯示404,而改成http://a.a.com/index.php/index/a即可訪問,

原因是他默認沒有開啓隱藏index.php,如何開啓我也沒查,不管了,能訪問就行了,

pen_basedir restriction in effect is not within the allowed path(s)

防跨站攻擊開啓導致的,thinkphp中public目錄下不需要開啓,直接關閉即可,而重要的app文件夾下是開啓的,可以防跨站攻擊的,也不用擔心安全問題

phpstudy + thinkphp6 添加站點配置

防跨站攻擊關閉
網站目錄到public目錄下
執行目錄爲空
其他就按默認配置即可

linux命令

ls -l 可以查看所屬用戶和用戶組

return new PDO($dsn, $username, $password, $params); PDOException in PDOConnection.php line 554

此錯誤是服務器的mysql 端口沒有打開導致的連接異常(一般爲3306端口)

Driver [Think] not supported.

需要安裝模板引擎
composer require topthink/think-view

靜態文件被重寫導致不能顯示

RewriteCond $1 !^(static|upload)
,htaccess文件中配置此項不重寫

Warning: require(/www/admin/q/public/…/vendor/autoload.php): failed to open stream: No such file or directory in /www/admin/q/public/index.php on line 15

Fatal error: require(): Failed opening required ‘/www/admin/q/public/…/vendor/autoload.php’ (include_path=’.:/usr/local/phpstudy/soft/php/php-7.2.21/lib/php’) in /www/admin/q/public/index.php on line 15

這是public 上一級目錄下的 vendor目錄不存在,可能是git經常自動忽略這個vendor,導致這個錯誤,如果缺失,複製一個過去即可

git 保存密碼不用每次輸入

$ git config --global credential.helper store

git 服務器拉代碼時Please, commit your changes or stash them before you can merge.

git reset --hard
git pull
放棄本地修改,以倉庫內容爲主

nginx 隱藏index.php入口文件

在網站的配置文件中加入下面代碼(與其他location配置平級的):
location / {
if (!-e KaTeX parse error: Expected '}', got 'EOF' at end of input: … rewrite ^(.*) /index.php?s=$1 last;
break;
}
}

nginx 與Apache選擇

nginx更高併發,Apache更穩定,本人喜好還是用nginx
因可以直接限制單個ip請求頻率,而Apache不支持,還需要thinkphp的中間件去完成,技術差.懶得學習中間件,就這麼湊合吧

常用方法

1. php中json轉list

son_decode($json,true)

2.jquery選擇器

let arr = $(this).parent(‘p’).children(‘input[name]’); 找到’p’的parent,然後找到子標籤中name爲input的標籤

3.thinkphp取useragent

$_SERVER[‘HTTP_USER_AGENT’]

php處理時間字符串的問題

date(‘Y-m-d H:i:s’, time()) date(‘Y-m-d h:i:s’, time()) 大寫H,一定要大寫,否則爲12小時制,返回的時間顯示會出問題

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章