Home  >  Article  >  PHP Framework  >  The difference between thinkphp5 and 3

The difference between thinkphp5 and 3

步履不停
步履不停Original
2019-07-01 11:33:443547browse

The difference between thinkphp5 and 3

First of all, let me state that this chapter is not a guide to upgrade old projects to 5.0, but is for developers using version 3.X to get familiar with and get started with this new version faster. At the same time, it is also strongly recommended that developers abandon their old thinking patterns, because 5.0 is a brand new subversive and reconstructed version.

3.X old ideas that need to be abandoned

Changes in URL

First of all We apologize for the incorrect guidance that the laxity of . Those that do not belong to $_GET can now be obtained through 'param'. The specific use can be queried through the request part.

Model changes

The new version of the model query returns the default 'object', and the system adds the 'toArray' method by default. Many developers use 'all' or 'select' 'Try to use 'toArray' to convert to an array. I hope developers can understand the concept of 'object', try to use 'object' to use data, or use the 'db' method to operate the database, and also remind you of this part' For developers who abuse 'toArray', the result of 'all' or 'select' is an array collection of objects, which cannot be converted using 'toArray'.

New version changes

Naming convention

Directory and file names use 'lowercase underscore', And start with a lowercase letter; class libraries and function files are uniformly suffixed with .php; class file names are defined in namespaces, and the path of the namespace is consistent with the path of the class library file (including upper and lower case); class names and class files The names should be consistent, and uniformly use camel case naming (the first letter is capitalized)

Function

The system no longer relies on any functions, but only provides assistants for commonly used operation encapsulation Function; the single-letter function is abandoned, and the system loads the assistant function by default. For details, please refer to the previous chapter 'Assistant Function';

routing

5.0 URL access no longer supports ordinary URL mode and routing do not support regular routing definitions. Instead, they are all changed to rule routing combined with variable rules (regular definitions). The details will not be described here.

Controller

The namespace of the controller has been adjusted, and there is no need to inherit any controller class.

The namespace of the application class library is unified as app (modifiable) instead of module name; the class name of the controller does not have the Controller suffix by default. You can configure the controller_suffix parameter to enable the controller class suffix; the controller operation method Use the return method to return data instead of direct output; abolish the original pre- and post-operation methods;

Version comparison

3.2 version Controller writing method

<?phpnamespace Home\Controller;use Think\Controller;class IndexController extends Controller 
{    public function hello()
   {        echo &#39;hello,thinkphp!&#39;;
   }
}

5.0 version controller writing method

namespace app\index\controller;class Index 
{    public function index()
   {        return &#39;hello,thinkphp!&#39;;
   }
}

3.2 version controller naming

IndexController. class.php

5.0 version controller naming

Index.php

**Correct output template in controller**

5.0 outputs the template in the controller. The usage method is as follows:

If you inherit think\Controller, you can use:

return $this->fetch(&#39;index/hello&#39;);

If your controller If you do not inherit think\Controller, use:

return view(&#39;index/hello&#39;);

Model

If you have to compare the improvements with the old version, the model is divided into databases The three parts, model and validator, respectively correspond to M method, model and automatic verification, and they have all been strengthened. A brief introduction is given below.

Database

The database query function of 5.0 has been enhanced. The chain query that originally needed to be used through the model can be called directly through the Db class. The M function call can use the db function instead, for example:

3.2 version

M(&#39;User&#39;)->where([&#39;name&#39;=>&#39;thinkphp&#39;])->find();

5.0 version

db(&#39;User&#39;)->where(&#39;name&#39;,&#39;thinkphp&#39;)->find();

Model

The new version of model query adds static methods, for example:

User::get(1); 
User::all();User::where(&#39;id&#39;,&#39;>&#39;,10)->find();

The model part has enhanced many functions, please refer to the "Model Chapter" for details.

Automatic verification

Compared with the old version, it can be understood as the previous automatic verification and different from the previous verification;

ThinkPHP5.0 verification uses an independent \think\Validate class or validator for verification. It is not only applicable to models, but can also be called directly in the controller. Please refer to the "Verification" chapter for specific usage rules, which will not be described here.

Configuration file

The new version has many configuration parameters or configuration levels that are different from before. It is recommended that you either take a look at the code, or Read the official development manual carefully and don't waste a whole day on configuration issues.

abnormal

5.0 has zero tolerance for errors. By default, exceptions will be thrown for any level of error, and the exception page has been redesigned to display detailed error information for easy debugging.

Abandonment of system constants

Compared with previous versions, the 5.0 version has made a large number of discards of system changes. If users have relevant needs, they can do so by themselves. Definition

The following are the abolished constants

REQUEST_METHOD IS_GET IS_POST IS_PUT IS_DELETE IS_AJAX __EXT__ COMMON_MODULE MODULE_NAME CONTROLLER_NAME ACTION_NAME APP_NAMESPACE APP_DEBUG MODULE_PATH etc.

Some constants can be obtained in Request, please refer to " Request a Chapter".

Note: Once again, this chapter is only written for developers who have previously used version 3.X to quickly understand 5.0. The specific functions of 5.0 require developers to read the manual thoroughly.

Assistant function

The comparison between the 5.0 assistant function and the single-letter function of version 3.2 is as follows:

C config

E exception

G debug

L lang

T abolish

I input

N abolish

D model

M db

A controller

R action

B abolish

U url

W widget

S ​​cache

F Abolish

For more ThinkPHP related technical articles, please visit the ThinkPHP Usage Tutorial column to learn!

The above is the detailed content of The difference between thinkphp5 and 3. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Is thinkphp open source?Next article:Is thinkphp open source?