Some little knowledge about thinkphp

炎欲天舞
Release: 2023-03-14 17:54:02
Original
1682 people have browsed it

When using thinkphp to build the backend, there are many parameters that need to be configured. Some of them cannot be remembered and have to be looked up. Here are some basic parameters and some fragmentary knowledge points. I hope I can share them with everyone. It can be convenient for everyone.

Friendly reminder: These configurations are for thinkphp3.2.3~~

1. Separate front and backend


//定义应用目录define('BIND_MODULE','Admin');drfine('App_PATH','./Application');
Copy after login

For For the security and ease of maintenance of a project, one entry file is usually used, and when the front and backend are separated, the code is changed in the configuration file when it is defined. A backend file entrance can appear, with the front and backend separated for easy management. You can also directly copy the Home folder and change the namespace in the controller and model.

2. Set the suffix of the view file


    'TMPL_TEMPLATE_SUFFIX' => '.php', // 默认模板文件后缀
Copy after login

3. Connect to the database


    'DB_TYPE'   => 'mysql', // 数据库类型
    'DB_HOST'   => 'localhost', // 服务器地址
    'DB_NAME'   => 'wish', // 数据库名
    'DB_USER'   => 'root', // 用户名
    'DB_PWD'    => 'root', // 密码
    'DB_PORT'   => 3306, // 端口
    'DB_PREFIX' => 'tp_', // 数据库表前缀
Copy after login

4. Simple and practical way to obtain server information


##

$info = array(
            '操作系统'=>PHP_OS,
            '运行环境'=>$_SERVER["SERVER_SOFTWARE"],
            '主机名'=>$_SERVER['SERVER_NAME'],
            'WEB服务端口'=>$_SERVER['SERVER_PORT'],
            '网站文档目录'=>$_SERVER["DOCUMENT_ROOT"],
            '浏览器信息'=>substr($_SERVER['HTTP_USER_AGENT'], 0, 40),
            '通信协议'=>$_SERVER['SERVER_PROTOCOL'],
            '请求方法'=>$_SERVER['REQUEST_METHOD'],
            'ThinkPHP版本'=>THINK_VERSION,
            '上传附件限制'=>ini_get('upload_max_filesize'),
            '执行时间限制'=>ini_get('max_execution_time').'秒',
            '服务器时间'=>date("Y年n月j日 H:i:s"),
            '北京时间'=>gmdate("Y年n月j日 H:i:s",time()+8*3600),
            '服务器域名/IP'=>$_SERVER['SERVER_NAME'].' [ '.gethostbyname($_SERVER['SERVER_NAME']).' ]',
            '用户的IP地址'=>$_SERVER['REMOTE_ADDR'],
            '剩余空间'=>round((disk_free_space(".")/(1024*1024)),2).'M',
        );
        $this->info=$info;
Copy after login

5. thinkphp constructor __ initialize() and __construct()

There is no __initialize() constructor in PHP's built-in functions. There is only the __construct() constructor. If the subclass has its own constructor (__construct()), it will call its own for initialization. If If not, call the constructor of the parent class to perform its own initialization. When both the subclass and the parent class have __construct() functions, if you want to call the parent class's __constrcut() at the same time when initializing the subclass, you can use parent:: __construct() in the subclass. .

The appearance of __ initialize() in ThinkPHP is only to facilitate programmers to avoid frequent use of parent:: __ construct() when writing subclasses, and at the same time correctly call the constructor of the parent class in the framework, so, we When initializing a subclass in ThnikPHP, use __ initialize() instead of __ construct().

6. redirect method

This method calls the U function to generate the actual URL redirect address, which is the same as the URL access in the project group, redirect across groups Jump just adds the concept of grouping project names. Routing can be used in redirect. The parameter usage of the redirect method is consistent with the usage of the U function. Please refer to the relevant parts of the U function to generate URL addresses. The difference between

and success/error:
redirect uses the PHP header redirection, while success/error uses the html meta http-equiv='Refresh' attribute jump.
redirect page without template, the output prompt information is directly output by echo within the function, while success/error has corresponding templates.
Redirect and success/error can both implement page jumps, but redirect can redirect without delay. Which one is used depends on the specific situation.

7. thinkphp file upload

When using thinkphp to set the directory for uploading files, an error may occur, indicating that the specified folder cannot be found, but In the configuration, savePath has been used to set it, but it just doesn't work.

The reason for the error is that it is not enough to just set the savePath parameter of upload. You must also set the root directory rootPath of upload. In other words, just set the rootPath parameters so that it can work.


 $upload=new \Think\Upload(); //实例化上传类
 $upload->maxSize=3145728;  //设置附件上传大小
 $upload->exts=array('jpg','gif','png','jpeg'); //设置附件上传类型
 $upload->savePath='./Public/Uploads/'; //设置附件上传目录
 //这个rootPath根目录千万不能忘记设置,要不savePath会找不到相应的文件夹
 $upload->rootPath='/.';
Copy after login

The above is the detailed content of Some little knowledge about thinkphp. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template