路由
隐藏index.php文件
默认的写法是 :[@]模块名/控制器名/方法名
那么匹配的路径就是:http://127.0.0.1/public/index.php/xx/index/dd
之前全路径是:http://127.0.0.1/public/index.php/index/index/index
现在想: http://127.0.0.1/public/index/index/index
1、打开apache重写模块
httpd.conf 文件
LoadModule rewrite_module modules/mod_rewrite.so
2、配置虚拟主机中允许重写
D:\devtool\phpstudy_pro\Extensions\Apache2.4.39\conf\vhosts\tkphp.com_80.conf
<VirtualHost *:80>
DocumentRoot "D:/devtool/phpstudy_pro/WWW/tp5.1"
ServerName tkphp.com
ServerAlias
FcgidInitialEnv PHPRC "D:/devtool/phpstudy_pro/Extensions/php/php7.3.4nts"
AddHandler fcgid-script .php
FcgidWrapper "D:/devtool/phpstudy_pro/Extensions/php/php7.3.4nts/php-cgi.exe" .php
<Directory "D:/devtool/phpstudy_pro/WWW/tp5.1">
Options FollowSymLinks ExecCGI
# 是否允许使用.htaccess文件
AllowOverride All
# 访问目录权限
Require all granted
DirectoryIndex index.php index.html error/index.html
</Directory>
</VirtualHost>
3、在入口文件中要有.htaccess文件
注意目录!!!是public下的.htaccess文件
二选一。总有一个是对的。
<IfModule mod_rewrite.c>
Options +FollowSymlinks -Multiviews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [L,E=PATH_INFO:$1]
RewriteRule ^(.*)$ index.php?/$1 [QSA,PT,L]
</IfModule>
隐藏public目录
设置自定义路由
路由有2种方式,一个是根据类名>方法名> 这种的,就是pathInfo类型。这种不推荐。麻烦。
还有是自定义路由,就是专门有一个文件定义好路由,这个推荐。
建议设置自定义 :
// 是否强制使用路由
‘url_route_must’ => true,
// 路由使用完整匹配
‘route_complete_match’ => true,
设置路由
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
use think\facade\Route;
/**
* 匿名路由
*/
Route::get('think', function () {
return 'hello,ThinkPHP5!';
});
/**
* 带参数的路由 http://127.0.0.1/hello/tom
*
* hello,tom
*/
Route::get('hello/:name', 'index/hello');
/**
* 普通路由。 千万别加 设置成 /index/index/hello
*/
Route::get('dd', 'index/index/hello');
return [
];
路由的分组
Route::group('分组名(字符串)或者分组路由参数(数组)','分组路由规则(数组或者闭包)');
Route::group('blog', function () {
Route::get(':id', 'read');
Route::post(':id', 'update');
Route::delete(':id', 'delete');
})->pattern(['id' => '\d+']);
# 分组的嵌套
Route::group(['method'=>'get'], function () {
Route::group('blog',function(){
Route::get(':id', 'read');
Route::post(':id', 'update');
Route::delete(':id', 'delete');
});
})->pattern(['id' => '\d+']);
通常的写法
/**
* 实际上请求 :http://www.tk.com/aa/indexxx 就是请求的是 http://www.tk.com/index/index/index
*
* 'name'=>'aa' 是自定义前缀
* 'prefix'=>'index/index/' 把全路径给提出来,方便
* indexxx 是路由名
* index 是实际要去找的方法名
*/
Route::group(['name'=>'aa','prefix'=>'index/index/'],function (){
Route::get('indexxx','index');
});
路由参数
1、路径上的参数
# 必填参数
Route::请求方式('路由表达式/:参数',匿名函数);
# 可选参数
Route::请求方式('路由表达式/[:参数]',匿名函数);
2、get参数/Post/通用参数
变量类型方法(‘变量名/变量修饰符’,’默认值’,’过滤方法’)
use think\facade\Request;
Request::get();
Request::post()
Request::param();
public function req(Request $request) {
// 门面方式获取数据 use think\facade\Request
/*
// GET的获取
#echo Request::get('id');
// 如果没有则使用默认值
#echo Request::get('age',20);
#echo Request::get('age',20,'intval');
// 获取get全部数据
#dump(Request::get());
// 判断一个key是否存在
#dump(Request::has('sex'));
// 获取指定的数据 白名单
#dump(Request::only(['id','age']));
// 排除不要的数据 黑名单
#dump(Request::except(['id']));
// POST获取
#dump(Request::post('name'));
// PUT获取
#dump(Request::put('name'));
// DELETE获取
#dump(Request::delete('name'));
// 获取任意类型
#dump(Request::param('name'));
// 判断请求的类型
dump(Request::isPost());
dump(Request::isGet());
dump(Request::isPut());
dump(Request::isDelete());
// 是否是ajax请求
dump(Request::isAjax());
// $_SERVER一样的
#dump(Request::server());
// 获取环境变量 说白了就是框架定义好的常量
//dump(Request::env());
// 获取路由
dump(Request::route());
*/
// 依赖注入方式 [推荐]
#$request = Req::instance(); # tp5.0有人这样来申明,但是我们推荐依赖注入
/*dump($request->get('name'));
dump($request->has('sex'));
dump($request->only(['id']));
dump($request->except(['id']));*/
}
3、input方法(推荐!!!)
好处,不用引入其他类
public function req2() {
// 辅助函数 [推荐]
// 获取GET的全部参数
#dump(input('get.'));
// 获取指定的数据
#dump(input('get.id'));
#dump(input('get.sex','女士'));
// post数据
//dump(input('post.'));
// 获取任意类型的请求
//dump(input('param.'));
//dump(input(''));
// 获取任意类型的 key 为name值 如果get和post优先post
//dump(input('name'));
// 判断一个key是值存在
#dump(input('?name'));
// 使用变量修饰符 a 数组 s 字符串 d 数字
#dump(input('id/d'));
}
控制器
使用命令行创建分组
php think build --module 分组名称
创建控制器
Ø 手动创建
application/模块目录/controller/目录下
命名规则:控制器名称(首字母大写) + (控制器后缀,默认没有) + .php
Ø 命令行方式创建【推荐】
php think make:controller --plain 模块名/控制器名
# 参数说明
--plain 标准控制器 (默认创建的控制器是一个资源控制器,所以一般加上此选项)
如果创建时,没有在模块名称,则默认创建到公共模块中 【common】
开启调试模式
默认情况下,错误描述比较模糊,不方便进行错误调试。这种模式通常叫做“部署模式”。
开发阶段可以将框架设置为调试模式,便于进行错误调试:
修改项目目录 config/app.php文件
‘show_error_msg’=false 改为 true;
再将
‘app_debug’ => false 改为 true;
实际开发中,使用第2种方案
APP_DEBUG=true
前置操作
可以为某个或者某些操作指定前置执行的操作方法,设置 beforeActionList属性可以指定某个方法为其他方法的前置操作,数组键名为需要调用的前置方法名,无值的话为当前控制器下所有方法的前置方法。
<?php
namespace app\index\controller;
use think\Controller;
class Index extends Controller
{
protected $beforeActionList = [
"aa" => ["index","hello"]
];
public function index()
{
return '<style type="text/css">*{ padding: 0; margin: 0; } div{ 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.1<br/><span style="font-size:30px">12载初心不改(2006-2018) - 你值得信赖的PHP框架</span></p></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="eab4b9f840753f8e7"></think>';
}
public function hello($name = 'ThinkPHP5')
{
return 'hello,' . $name;
}
protected function aa(){
echo "我是前置方法aa";
}
protected function bb(){
echo "我是前置方法bb";
}
}
页面跳转
默认跳转成功的页面
如果你觉得默认太丑,就可以修改这两个文件
在应用开发中,经常会遇到一些带有提示信息的跳转页面,例如操作成功或者操作错误页面,并且自动跳转到另外一个目标页面。系统的\think\Controller类内置了两个跳转方法success和error,用于页面跳转提示。
1.跳转全路径
前提是需要在配置文件中把这个匹配模式打开。不然访问都访问不到
public function index2(){
return $this->success("登录成功","index/index/su");
//简单写法
//同一个模块,同一个控制旗下 可以直接写 方法名
return $this->success("登录成功",url("su"));
//同一个模块,不同的控制器
return $this->success("登录成功",url("demo2/index"));
// 不用的模块 不同的控制器
return $this->success("登录成功",url("index/index/index"));
}
public function su(){
return "我是成功页面";
}
2.自定义路由(推荐!!!)
必须给路由起一个别名!!!!!!
访问:http://www.tk.com/index2 跳转到 http://www.tk.com/aaa.html
设置自定义路由,然后起个别名。
Route::get('index2', 'index/index/index2');
Route::get('aaa', function () {
return 'zzzzzzzzzzzzzz';
})->name('zdy');
然后跳转。
return $this->success("登录成功",url("zdy"));
3.错误页面
return $this->error("我错了");
4.ajax请求
很牛逼,自动识别是返回json还是页面。
页面
接口
public function ajax(){
return $this->success("登录成功",url("index/index/ajax"),['staus' => 1]);
}
5.第二种ajax返回(推荐)
由于默认是输出Html输出,所以直接以html页面方式输出响应内容,但也可以设置默认输出为json格式
‘default_return_type’ => ‘json’,
返回json数据,
return json($data,http状态码);
数据,状态码,返回的自定义请求头数据
return json($data,201,['username'=>'admin','password'=>'admin888']);
重定向
可以使用redirect助手函数进行重定向
redirect('地址或方法名',数组参数);
推荐使用这个!!!
return redirect(url('index/index/req2', ['id' => 1]));