Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common functions of thinkphp6

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, the editor will share with you the relevant knowledge points about the common functions of thinkphp6. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

The first step is to change the composer image

Ali image: composer config-g repo.packagist composer https://mirrors.aliyun.com/composer/

Laravel China image: composer config-g repo.packagist composer https://packagist.laravel-china.org

Full mirror image in China: composer config-g repo.packagist composer https://packagist.phpcomposer.com9

It is found that it cannot run without vendor in the download case of Ciyun or git.

Composer install-ignore-platform-reqs

Or

Composer update-ignore-platform-reqs

Tp6 usage

1. Download tp6

Composer create-project topthink/think tp

2. Set the multi-application mode

Tp6 is the default method for single application access to enter app/controller by default. If you need to do multi-application development (e.g. http://***.com/admin, http://***.com/index), you need to enable multi-application mode.

Composer requiretopthink/think-multi-app

Name of php think build application (example: index or admin)

3. Template rendering

Tp3: $this- > display (); tp5: return $this- > fetch (); tp6: return View::fetch ('index')

Tp6 lacks many dependent packages by default and needs to be downloaded.

Composer require topthink/think-view

Controller introduction

Use think\ facade\ View

4. Template jump redirection

Composer require liliuwei/thinkphp-jump

Controller introduction

Header introduction: use\ liliuwei\ think\ Jump

In-class introduction: use Jump

If an error is reported:

Check to see if app/config/jump.php has settings:

'dispatch_success_tmpl' = > app ()-> getRootPath (). / vendor/qeq66/think-jump/template/jump.html','dispatch_error_tmpl' = > app ()-> getRootPath (). / vendor/qeq66/think-jump/template/jump.html'

5. Get form data

Controller introduction

Use think\ facade\ Request;$code = Request::param ('code')

Or

$code = input ("code")

6. Digital verification code

Composer require topthink/think-captcha

Locate the global middleware middleware.php file in the application app directory and open the code noted below\ think\ middleware\ SessionInit::class

7. Upload pictures and process pictures

Composer require topthink/think-image

8. Mysql select query

For transitions from tp5, the default select query returns a two-dimensional array and tp6 returns a dataset, although officially it is basically the same as an array operation

But sometimes the array is easy to use, such as arr [k] [0] = "test". In the default returned dataset, an error is reported, but the array can be operated in this way.

1 、

Db::name ('ceshi')-> select ()-> toArray ()

2 、

Modify the BaseQuery.php of / vendor/topthink/think-orm/src/db in the tp6 directory

The modification example is shown in the figure, deleting the location of the red box in the figure, and in the

$resultSet = $this- > connection- > select ($this)

Add a line below

Return $resultSet

9. Paging

$list = db::name ('admin_menu')-> where ($where)-> paginate ([' list_rows'= > 10 request ()-> param (),])

Use the paginate method to get paged data and query the subscript values that cannot be added to the collection.

The query condition needs to be added 'query' = > request ()-> param ()

Solution:

Php side:

/ / An highlighted block$list = db::name ('admin_menu')-> where ($where)-> paginate ([' list_rows'= > 10 array query'= > request ()-> param (),]); $new_arr = array (); foreach ($list as $k = > $v) {$v [$k] ['erji_menu'] = "case"; $new_arr [] = $v;} / get paging display $page = $list- > render (); / template variable assignment View::assign (' list', $new_arr) View::assign ('page', $page)

Html end

{$page | raw}

Paging reference class modification

Tp6\ vendor\ topthink\ think-orm\ src\ paginator\ driver\ Bootstrap.php

10. New data differences

Contrast the tp5 save method for updating the add method for adding

Tp6 save is both an update and an addition add method is deleted

Db::name ('ceshi')-> where (array (' tz_id'= > $post ['tz_id']))-> save ($users); db::name (' ceshi')-> save ($users)

Add the primary key where is updated, do not add the primary key is added, but there will be problems. If the execution succeeds, it will only return 0Magne1.

Does not return the primary key id that adds data like the tp5 add method

/ * * insert record * @ access public * @ param array $data data * @ param boolean $getLastInsID returns the self-incremented primary key * @ return integer | string * / public function insert (array $data = [], bool $getLastInsID = false) {if (! empty ($data)) {$this- > options ['data'] = $data;} return $this- > connection- > insert ($this, $getLastInsID);}

The add method has been changed to the insert method. Although the comment says return the self-incrementing primary key, I still didn't get the self-incrementing primary key on my side of the test. I don't know if it's a version problem or something. If you don't encounter it, forget it.

/ * insert the record and get self-incremented ID * @ access public * @ param array $data data * @ return integer | string * / public function insertGetId (array $data) {return $this- > insert ($data, true);}

Then I had to keep looking and found that there was an insertGetId that I could get. In other words, in order to develop quickly, we usually use save to solve the problem, and if you want to use the self-increasing primary key, change it to insertGetId.

11. Tp6 Advanced query and and or are used simultaneously

Tp6 has quick methods for and and or queries

However, these shortcuts have many limitations, and when we make a series of complex queries on the data, there will be a lot of problems, such as:

$where1 [] = ["order_khname", "like",'%'. $keywords.'%']; $where2 [] = ["order_khqq", "like",'%'. $keywords.'%']; $where3 [] = ["order_khmobile", "like",'%'. $keywords.'%']; $where [] = ["order_type", "=", $ddzt] $list = db::name ('ceshi')-> where ($where)-> whereOr ([$where1,$where2,$where3])-> paginate ([' list_rows'= > 10, 'query' = > request ()-> param (),])

The sql format we want here is:

SELECT * FROM `dc_ ceshi` WHERE `order_ type` = 0 and ((`order_ khname` LIKE'% 1%') OR (`order_ khqq` LIKE'% 1%') OR (`order_ khmobile` LIKE'% 1%') LIMIT 0Mag10

The actual generated format is:

SELECT * FROM `dc_ ceshi` WHERE `order_ type` = 0 OR (`order_ khname` LIKE'% 1%') OR (`order_ khqq` LIKE'% 1%') OR (`order_ khmobile` LIKE'% 1%') LIMIT 0Magol 10

Closure query can be used here (Note: this is just a problem I encountered when I was learning tp6 and then found a solution in the tp6 document. It can also be solved by using native sql directly, changing the where condition directly to a custom string, and using tp5 connection method, but I didn't try)

$list = db::name ('ceshi')-> where (function ($query) use ($keywords) {$query- > where ("order_khname", "like",'%'. $keywords.'%')-> whereOr ("order_khqq", "like",'%'. $keywords.'%')-> whereOr ("order_khmobile", "like",'%'. $keywords.'%') })-> where ($where)-> paginate (['list_rows'= > 10,' query' = > request ()-> param (),])

At this time, the sql format is as follows:

SELECT * FROM `order_ ceshi` WHERE (`order_ khname` LIKE'% 1%'OR `order_ khqq` LIKE'% 1%'OR `order_ khmobile` LIKE'% 1%') AND `order_ type` = 0 LIMIT 0J10

My needs have been met, but there is still a problem, that is, if the variable keywords has no value, the format of the query statement is:

SELECT * FROM `order_ ceshi` WHERE (`order_ khname` LIKE'%%'OR `order_ khqq` LIKE'%%'OR `order_ khmobile`ceshi`%') AND `order_ type` = 0 LIMIT 010

In this case, you only need to add an if to judge:

$list = db::name ('ceshi')-> where (function ($query) use ($keywords) {if ($keywords) {$query- > where ("order_khname", "like",'%'. $keywords.'%')-> whereOr ("order_khqq", "like",'%'. $keywords.'%')-> whereOr ("order_khmobile", "like",'%'. $keywords.'%') }})-> where ($where)-> paginate (['list_rows'= > 10,' query' = > request ()-> param (),])

12. Tp6 find query changes

Tp5, tp3 We query whether a table has data, we can query a table directly.

$datafind = db::name ('ceshi')-> find ()

Tp6 find query must add where condition or order sort

$datafind = db::name ('ceshi')-> order (' ID DESC')-> find (); that's all of the article "what are the common functions of thinkphp6?" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report