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
的时候可以看到
ok!这里表示我们使用路由
访问成功了~!
最基础的路由定义方法是:
Route::rule(‘路由表达式’, ‘路由地址’, ‘请求类型’);
路由都包括
类型 | 描述 | 快捷方法 |
---|---|---|
GET | GET请求 | get |
POST | POST请求 | post |
PUT | PUT请求 | put |
DELETE | DELETE请求 | delete |
PATCH | PATCH请求 | patch |
* | 任何请求类型 | any |
下面我会详细见解下如何使用路由
访问控制器
的类
控制器
执行命令
php think make:controller Article
这个时候我们可以看到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
看到
恭喜恭喜,你已经掌握了
tp
的路由
控制器
让我们继续!
视图
默认系统下
view
文件下有个README.md
文件里面说到如果不使用模板,可以删除该目录
为什么呐?因为现在后端基本都写接口
基本不会去写view
层(真香)
让我在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}
刷新~!
完美~!
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: