ThinkPHP 6.0 基础教程 - 基本流程

路由

tp默认的路由在route/app.php文件下,我们可以看到如下内容:

use think\facade\Route;

Route::get('think', function () {
    return 'hello,ThinkPHP6!';
});

Route::get('hello/:name', 'index/hello');

当我们访问http://127.0.0.1:8000/think的时候可以看到

ThinkPHP 6.0 基础教程 - 基本流程
ok!这里表示我们使用路由访问成功了~!
最基础的路由定义方法是:

Route::rule(‘路由表达式’, ‘路由地址’, ‘请求类型’);

路由都包括

类型 描述 快捷方法
GET GET请求 get
POST POST请求 post
PUT PUT请求 put
DELETE DELETE请求 delete
PATCH PATCH请求 patch
* 任何请求类型 any

下面我会详细见解下如何使用路由访问控制器的类

控制器

执行命令

php think make:controller Article

ThinkPHP 6.0 基础教程 - 基本流程

这个时候我们可以看到tp给我们默认生成了一些类

<?php
declare (strict_types = 1);

namespace app\admin\controller;

use think\Request;

class Article
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}

如果你不想要这些默认类的话在后面加上--plain即可

php think make:controller Article --plain

此时我们操作下路由访问Article控制器,比如说我们访问Article控制器的index方法。

// app/controller/Article.php

public function index()
{
    return 'Hello World';
}
// route
Route::get('article', 'article/index');

当我们访问http://127.0.0.1:8000/article看到

ThinkPHP 6.0 基础教程 - 基本流程

:stuck_out_tongue_winking_eye: 恭喜恭喜,你已经掌握了tp路由 控制器让我们继续!

视图

默认系统下view文件下有个README.md文件里面说到如果不使用模板,可以删除该目录为什么呐?因为现在后端基本都写接口基本不会去写view层(真香):laughing:

让我在view新建article/index.html文件

Article控制器

// Article
public function index()
{
    return view('index', [
    'name'  => '假如',
    'email' => '897645119@qq.com'
    ]);
}

此时我们去访问http://127.0.0.1:8000/article会提示错误:Driver [Think] not supported.
看不到错误的小伙儿可以在config/app.php

// 显示错误信息
'show_error_msg'   => true,

执行下命令

composer require topthink/think-view

ok,在刷新我们会看到一个空白页面,此时需要给index.html加点料

{$name} - {$email}

刷新~!

ThinkPHP 6.0 基础教程 - 基本流程

完美~!

本作品采用《CC 协议》,转载必须注明作者和本文链接
与其感慨路难行,不如马上出发。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!