Thinkphp5.0 Quick Start Notes (1)

 

Learning sources and instructions

https://www.kancloud.cn/thinkphp/thinkphp5_quickstart

Testing and deployment are learning in windows10.

Composer install and update

Composer is a PHP tool for managing dependence (dependency) relationships. Can declare their project depends external tool library (libraries), Composer will help you install these dependencies libraries.

URL: https://www.phpcomposer.com/

Download: https://getcomposer.org/Composer-Setup.exe

ThinkPHP framework Download

[Github]
application projects: https://github.com/top-think/think
core framework: https://github.com/top-think/framework
[code cloud]
application projects: https: //git.oschina. net / liu21st / thinkphp5.git
core framework: https://git.oschina.net/liu21st/framework.git
[Coding]
application projects: https://git.coding.net/liu21st/thinkphp5.git
core framework: https : //git.coding.net/liu21st/framework.git

Deploying and testing

The deployment process using xampp build.

XAMPP (Apache + MySQL + PHP + PERL) station is a powerful integrated package.

After downloading the default installation. After installation opening xampp-control, the following interface. Click the start Apache and MySQL start, allowing the network , enter 127.0.0.1 test.

If Apache can not start to consider issues occupied port, using port 80, 443. In the cmd window command: netstat -ano view the port occupancy, then find the corresponding process PID occupy the port in the Task Manager, end off, restart Apache to try.

After testing, httpd.conf file under the Apache Config is opened (the last line of about 568) in the text editor enter the last:

<VirtualHost *:80>
    DocumentRoot "C:\xampp\htdocs\server\public"
    ServerName www.server.com
</VirtualHost>

 And then C: establishing at \ xampp \ htdocs folder server, the frame php extract to the folder, checking out the corresponding public folder.

 Then in the C: \ Windows \ System32 \ drivers \ etc, find the host file, modify the properties, so that you can edit, and add in the final end:

192.168.0.103  www.matlabserver.com

So that the network may correspond to a domain name by ip. Wherein the native 192.168.0.103 ip address corresponding to the LAN, the user should use ipconfig in cmd window, find the corresponding LAN ip address itself.

The last test, a browser, enter the ip, the effect after the testing is completed as follows :( Of course, now thinkPHP went forth to version 6.0, if you use version 5.1 or 6.0, it should be slightly different results, but does not affect it)

Demo, controller, view, debugging

demo

Open cmd window frame ThinkPHP root path, input

php think build --module demo

会在application/下生成demo代码作为示例。

控制器

控制器位于路径application/index/controller/Index.php。编辑该文件即对主页编辑。控制器的路径和public/index.php配置有关。

修改application/index/controller/Index.php文件,则修改了主界面,如:

<?php
namespace app\index\controller;
class Index
{
    public function index()
    {
        return 'Hello,World!';
    }
}

 

视图

和demo一样,在application/index/下创建view文件夹,如图:

在之下创立hello.html,有如下内容:

<html>
    <head>
     <title>hello {$name}</title>
    </head>
    <body>
     hello, {$name}!
    </body>
</html>

 修改application/index/controller/Index.php控制器为以下,则通过hello相互关联,控制器添加视图文件功能。(使用use声明继承方便,不适用use则需要class Index extends \think\Controller声明继承。

<?php
namespace app\index\controller;
use think\Controller;
class Index extends Controller
{
    public function hello($name = 'thinkphp')
    {
        $this->assign('name', $name);
        return $this->fetch();
    }
}

 效果如下:(注意url访问 http://serverName/index.php/模块/控制器/操作)

调试开关

调试开关位于application/config.php,第20行左右修改为以下,则关闭了调试开关:

    'app_debug'              => false,

打开状态:

关闭状态:

与数据库连接

 数据库在xampp下的打开方式,可以通过MySQL的admin打开,也可以在cmd窗口中操作。下图为在xampp下的打开mysql方式。

 

在cmd中操作,首先将环境变量添加到path中,然后再打开cmd窗口。下图为添加环境变量示例。

然后输入如下,进入mysql。

输入以下,创建id,data的数据库,插入三条数据。

show databases;

create database demo;
use demo
CREATE TABLE IF NOT EXISTS `think_data`( `id` int(8) unsigned NOT NULL AUTO_INCREMENT, `data` varchar(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 ; INSERT INTO `think_data`(`id`,`data`) VALUES (1,'thinkphp'), (2,'php'), (3,'framework');

select * from think_data;

一点效果:

 

在application/database.php中修改文件内容为:

<?php

return [
    // 数据库类型
    'type' => 'mysql',
    // 服务器地址
    'hostname' => '127.0.0.1',
    // 数据库名
    'database' => 'demo',
    // 数据库用户名
    'username' => 'root',
    // 数据库密码
    'password' => '',
    // 数据库连接端口
    'hostport' => '',
    // 数据库连接参数
    'params' => [],
    // 数据库编码默认采用utf8
    'charset' => 'utf8',
    // 数据库表前缀
    'prefix' => 'think_',
    // 数据库调试模式
    'debug' => true,
    ];

 修改控制器代码为:

<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
class Index extends Controller
{
    public function index()
    {
        $data = Db::name('data')->find();
        $this->assign('result', $data);
        return $this->fetch();
    }
}

 添加模板文件view/index.html,设立内容为:

<html>
    <head>
        <title></title>
    </head>
    <body>
        {$result.id}--{$result.data}
    </body>
</html>

 保存,最终效果为:

 成功连接上了数据库,然后输出第一条数据。

 也可以尝试更改控制器代码第九行为:

        $data = Db::name('data')->where('id',2)->find();

 输出数据库第二条数据。

参阅地址:https://www.kancloud.cn/manual/thinkphp5/135176

 

Guess you like

Origin www.cnblogs.com/bai2018/p/11291674.html