Home  >  Article  >  Backend Development  >  What you need to know to get started with thinkphp5

What you need to know to get started with thinkphp5

dream
dreamOriginal
2017-07-02 09:49:161940browse

1: Module design

Single module is used by default, single module is supported, and all modules use app as the root namespace

You can create a common module, but it cannot be accessed directly. You can use it in other modules access, improve security

common class can be inherited arbitrarily

2: tp5 configuration

1 Conventional configuration

Define configuration directory

In the entry file

define('CONF_PATH',__DIR__.'/../conf/');

2 Application configuration

In the previous section directory Create a new config.PHP in

return array();

The array contains configuration items

3 Extended configuration

Split different configuration files into For configuration files that cannot be used for easier management, create an extended configuration folder extra

in the config directory and then create a new configuration file directly

For example, qiniu.php

The configuration format is the same as above

tp5 allows database configuration to be placed directly under the config folder (at the same level as config.php)

4 Scenario configuration

Different configurations used in the same scenario

Configure app_status in config.php, and then create the configuration of the corresponding environment in extra

5 Module configuration

Create a folder with the same name as the module in the config folder , and then create a new config.php file

The module configuration directory is consistent with the config directory. You can create an extra extension configuration. The extension configuration only works under the corresponding module

6 Dynamic configuration

a. The configuration items using the helper function config in the __construct constructor in the controller can take effect in the controller

b. The newly created configuration items using the helper function in the method can only be used in the modified method. Effective

7 Use of config class

Dynamicly change the config configuration, you can use the helper function config() and the config class

config('key','value', 'Scope')

Judge whether key exists

config('?key');

8 Environment variable configuration and use

Three: Routing

1 Entry file

public/index.php Single entrance

2 Hidden entry file

is both hidden index.php

a Enable apache's rewrite module, allowed all

b .htaccess configuration exists in the public directory, no need to configure it yourself

2 Entry file binding

Binding module name, Shorten uri

For example, to bind the background module

Define in admin.php difine('BIND_MODULE','admin'); Bind the background module

difine(' BIND_MODULE','admin/Index'); Bind the background Index controller

Set 'auto_bind_module'=>true in the configuration file, then you don't need to define difine('BIND_MODULE','admin') ;

3 Routing

Enable routing first (already enabled by default) and configure 'url_route_on'=>true,

'url_route_must'=>false ( If it is true, routing must be configured to access)

Create a new routing file route.php in the configuration file

Write routing rules directly

For example, return array(

'news/:id'=>'index/Index/news',

);

Four: Request

1 request Request

a Helper function request()

b Thinkphp’s request class

c Object injection, inject the object into the method

2 Request object request

a Get the link in the browser

Get the domain name$request->domain()

Get the pathinfo $request->pathinfo()

Get the path $request- >path()

b Request type

Type of current request $request->method();

Determine whether it is a get request $request->isGet ()

Determine whether it is an ajax request $request->isAjax()

---Determine whether it is a mobile phone $request->isMobile();

c Request parameters
Get the parameters after? $request->get()

Get all parameters (including those in path) $request->param()

Get post request $request->post()

Get session $request->session()

Get cookie $request->cookie()

d Get Module, controller, operation

$request->module();
$request->controller();
$request->action();
3 input Assistant function

is equivalent to the I function in tp3.2

input('post.name')

input('get.name')

input('cookie.name)

input('session.name)

...

You can get various parameters in the request

4 Response object

config('default_return_type','json'); Perform return operation by dynamically modifying the return parameter type

More suitable for api encapsulation

type type Support json, xml, html (default)

For example:

config('default_return_type','json');
$res = ['code'=>200,
'data' = & gt; [1,2,3,4,5,6,7,8,9]
];
Return $ res; Data

Five views

Direct display

return view();The default is app/group name/view/controller name/method name.html

Pass the first parameter

return view('name') default Pass the first parameter for app/group name/view/controller name/name.html

return view('name1/name2') The default is app/group name/view/ name1/name2.html

return view('./index.html') defaults to the index,.html file in the same directory as the index.php entry file


Transfer value to the template

The second parameter is in array form

return view('name',['key'=>'value'])

The above is the detailed content of What you need to know to get started with thinkphp5. 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