ThinkPHPV5.0.3 – 1

简介:ThinkPHP V5.0——为API开发而设计的高性能框架

安装:

当前系统Mac os,所有在命令行里边进行了安装,安装的不是核心包,而是项目包。

官方给出三种安装方法,我用了第一种方式:

1,首先安装Composer,Composer是PHP的一种依赖库。

安装依赖库两条命令:

curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
第一条执行之后命令台输出:
All settings correct for using Composer
Downloading 1.2.3...
 
Composer successfully installed to: /Users/Ashes/composer.phar
Use it: php composer.phar
第二条执行之后命令台输出:

mv: rename composer.phar to /usr/local/bin/composer: Permission denied 就是当前/usr/local/bin/目录没有修改的权限,修改一下权限就行。

2,然后将当前目录切换到PHP文件目录下,也就是www目录下,执行:
composer create-project topthink/think tp5  --prefer-dist
等一段时间,控制台输出:
Installing topthink/think (v5.0.3)
  - Installing topthink/think (v5.0.3)
    Downloading: 100%         
 
Created project in tp5
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing topthink/think-installer (v1.0.11)
    Downloading: 100%         
 
  - Installing topthink/framework (v5.0.3)
    Downloading: 100%         
 
Writing lock file
Generating autoload files

3,然后执行:http://localhost/tp5/public/

执行可能会出现错误,那是文件夹权限的问题,修改文件夹以及子文件夹权限。

chmod -R 777 currentFinder/

安装完毕之后,大体了解了一下这个框架,这个框架相对于之前的版本变更的还是比较厉害的,首先控制器不继承Controller了,其次有好多类方法返回数据都有相应的变化。甚至还有一些命名规范。

首先根目录下application文件夹是当前项目的文件夹,里边包含了公共文件,配置文件,数据库连接配置文件,还有一些初始化定义的参数。

1,连接数据库: database.php,常用的一些设置,

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

2,config.php是系统一些基本配置,根据自己需求去配,没怎么仔细研究。

3,框架的基本结构,这个基本能够实现对接app接口的功能。

application文件夹下默认创建了一个index模块,index下边有一个controller,也就是控制器文件夹,以前的框架里边还有另外两个文件夹,一个view,也就是模板文件,一个model,模型。

进来index/controller/Index.php,里边的代码有一个index模块,模块下有一个index方法:

<?php
namespace app\index\controller;
 
class Index extends Controller
{
    public function index()
    {
        return '<style type="text/css">*{ padding: 0; margin: 0; } .think_default_text{ padding: 4px 48px;} 
a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } 
body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px} 
h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style>
<div style="padding: 24px 48px;"> <h1>:)</h1><p> ThinkPHP V5<br/><span style="font-size:30px">十年磨一剑 - 为API开发设计的高性能框架</span></p>
<span style="font-size:22px;">[ V5.0 版本由 <a href="http://www.qiniu.com" target="qiniu">七牛云</a> 独家赞助发布 ]</span></div>
<script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>
<script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_bd568ce7058a1091"></thinkad>';
    }
    
}

namespace是对当前空间进行命名。

当前方法直接返回的是一段html字符串,并且输出在了浏览器中,如果是接口,应该返回json,直接返回。

当前模块下,当前控制器的index方法,在浏览器中访问:http://localhost/app/public/index.php/index/index  简化为:

http://localhost/app/public

http://localhost/app/public/index/index/

添加一个hello方法:

public function hello()
    {
        return "hello world!";
    }
    //访问地址为:http://localhost/app/public/index.php/index/index/hello
                http://localhost/app/public/index/index/hello

如果在index模块下新添加一个控制器,Index2.php:

//不同控制器的不同方法的创建和访问。
<?php
namespace app\index\controller;
 
class Index2
{
    public function index()
    {
        $data = ['name'=>'thinkphp','url'=>'thinkphp.cn'];
        return json_encode(['data'=>$data,'code'=>1,'message'=>'操作完成']);
    }
    public function hello()
    {
        return "123";
    }
    public function hi()
    {
        return "ashes of time";
    }
}
//三个方法的访问链接分别为:
http://localhost/app/public/index/index2/
http://localhost/app/public/index/index2/hello
http://localhost/app/public/index/index2/hi

创建新的模块,在当前应用中:

在 application下边新建一个admin文件夹,代表了当前新模块名儿,在admin下边新建一个controller文件,保存控制器文件,现在创建了一个Index.php:

<?php
//命名空间就应该是admin下边的控制器了。
namespace app\admin\controller;
 
class Index
{
    public function index()
    {
        return "This is Admin Module!";
    }
 
    public function login()
    {
        return "This is login!";
    }
    
}
//访问URL分别为:http://localhost/app/public/admin || http://localhost/app/public/admin/index/index
http://localhost/app/public/admin/index/login

以上是框架路径基本的访问以及模块的访问创建。

Leave a Reply

Required fields are marked *