1. Download
Download Address: http://www.thinkphp.cn/
This time using THINKPHP5, I use GitHub to install.
Github
Application Item: Https://github.com/top-think/think
Core framework: Https://github.com/top-think/framework
In addition, there are:
Code Cloud:
Application Item: Https://git.oschina.net/liu21st/thinkphp5.git
Core framework: Https://git.oschina.net/liu21st/framework.git
Coding:
Application Item: Https://git.coding.net/liu21st/thinkphp5.git
Core framework: Https://git.coding.net/liu21st/framework.git
Download the completed directory:
Tp5├─application application directory ├─extend extension Class Library directory (definable) ├─public Web site access directory ├─runtime runtime Catalog (definable) ├─vendor Third-party Class library catalog (Composer) ├─thinkphp Framework Core Catalog ├─build.php Auto-Generate definition file (reference) ├─composer.json Composer definition file ├─license.txt Authorization Notes file ├─readme.md README file ├─think command line tool entry
The structure of the core framework directory is as follows:
├─thinkphp Framework System catalog │├─lang Language Pack directory │├─library Framework Core Class Library Catalog ││├─think think class Library Package directory ││└─traits System traits directory │├─tpl System template directory │││├─.htaccess for Apache The rewrite │├─.travis.yml CI definition file │├─base.php Framework base File │├─composer.json composer definition file │├─console.php Console Portal File │├─convention.php custom configuration file │├─helper.php Helper function file (optional) │├─license.txt Authorization note File │├─phpunit.xml Unit test configuration File │├─readme.md README file │└─start.php Framework boot file
2. Running
I am using Kali's own apache2 server, using service apache2 start
boot, need to put git down the entire project into the server running directory, linux default is:
/var/www/html
Then enter it on the browser side: http://localhost/tp5/public/
You can see the Welcome page:
If you don't want to install any WEB server, you can also use PHP's own WebServer and run router.php to run your tests.
Go to the command line and enter the Tp5/public directory, entering the following command:
Php-s localhost:8888 router.php
Next you have direct access to
http://localhost:8888
3. Directory structure
The most common concern is the application directory:
├─application app directory (can be set) │├─index Module catalog (can change) ││├─config.php module configuration file │ │├─common.php Module Common file ││├─controller Controller directory ││ ├─model Model Catalog ││└─view View Catalog │││├─command.php command-line tool configuration file │├─common.php Apply a common file │├─config.php application profile │├─tags.php Apply the behavior extension definition file │├─database.php database configuration file │└─route.php Routing configuration File
Version 5.0 has a modular design architecture, the default application directory below is only one index module directory, if you want to add a new module can be generated using console commands. Switch to command-line mode, go to the app root (tp5 below) and execute the following command:
PHP think build --module Demo
A default demo module is generated, including the following directory structure:
├─demo│ ├─controller Controller Catalog │ ├─model model Catalog │ ├─view View catalog │ ├─config.php Module Profile │ The └─common.php module common file also generates a default Index controller file.
4. Template rendering
The first is the controller:
is located application/index/controller/Index.php
with a default index class:
Originally it was return to the start page and now to Hello World.
<?phpnamespace app\index\controller;class index{public Function Index () { return ' hello,world! '; }}
Then we inherit the controller class:
<?phpnamespace App\index\controller;use think\controller;//introduces Controller class index extends controller{ Public Function Index ($name = ' world ') { $this->assign (' name ', $name); return $this->fetch ();} }
We pass a parameter with a default value to the page name.
Then the view:
Thinkphph with template rendering, template exists under the View folder, default is no view folder, we create:
application/index
Create a view directory below the directory, build an index directory under the View directory, and then add the template file hello.html, the entire path:view/index/hello.html
Then we can access:
Or take the omitted path: http://localhost/tp5/public/
More advanced routes that can configure URLs.
5. Accessing the database
The MySQL database is used here and a database is built under the test table:
CREATE table if not exists think_data (ID int (8) is not NULL Auto_increment primary key, data varchar (255) is not null) engine=m Yisam default Charset=utf8;
Insert a few more data on the line;
Then application/database.php
configure the following:
return [ //Database type ' type ' = ' mysql ',// server address ' hostname ' = ' 127.0.0.1 ', //database name ' database ' = ' Test ',// user name ' username ' + ' root ', //password ' password ' = > ', //Port ' hostport ' + ',// connect DSN ' dsn ' = ' , //database connection Parameters ' Params ' = = [], //Database encoding defaults to UTF8 ' charset ' = ' utf8 ' , //database table prefix ' prefix ' = > ' think_ ', ///Database Debug mode ' Debug ' =-true,
Modify the Index class under controller:
<?phpnamespace app\index\controller;use think\controller;use think\db;//Introduction Database class Index extends controller{ Public Function Index ($name = ' world ') { $this->assign (' name ', $name); return $this->fetch (); } Public Function dbtest () { $data = db::name (' data ')->find (); $this->assign (' result ', $data); return $this->fetch ();} }
Then create a dbtest.html render in the index directory under view:
can be accessed again http://localhost/tp5/public/index.php/index/index/dbtest
.
This article explains the ThinkPHP5 Quick Start method, more relevant content please pay attention to the PHP Chinese web.