thinkphp url和路由
2024-08-31 21:37:03
一.入口模块修改
修改public下的index 加入 define('BIND_MODULE','admin'); 即可将入门模块绑定到admin模块
<?php // [ 应用入口文件 ] // 定义应用目录
define('APP_PATH', __DIR__ . '/../application/'); //定义配置文件目录
define('CONF_PATH',__DIR__.'/../conf/');
define('BIND_MODULE','admin');
// 加载框架引导文件
require __DIR__ . '/../thinkphp/start.php';
二.路由美化 在conf 目录创建route.php
1.开启路由配置
// 是否开启路由
'url_route_on' => true,
// 是否强制使用路由
'url_route_must' => false,
2.在conf目录新建 route.php
<?php return [
'new/:id' => 'admin/index/index',
];
3.助手函数 url()直接修改url
public function index($id){
echo url('admin/index/index',['id'=>11]).'<br />';
return "index";
}
4.route.php 路由规则
'news/' => 'admin/index/index', // 添加路由规则 路由到 index控制器的index操作方法
http://localhost/index
'news/:id' => 'admin/index/index', //该路由规则表示所有index开头的并且带参数的访问都会路由到index
控制器的index操作方法,且参数不能为空
http://localhost/index?id=6&name=dd
'news/[:id]' => 'admin/index/index', // 路由参数name为可选
http://localhost/index?id=7 或 http://localhost/index 均可
动态路由
/* 动态路由设置 */
use \think\Route;
/* 将Index控制器设置别名为斜杠 / */
Route::alias('test','\app\index\controller\Test'); /* 设置路由规则 */
Route::rule([
'news/:id' => 'index/test/index',
'blog/:id' => ['Blog/update',['ext'=>'html'],['id'=>'\d{4}']],],'','GET',['ext'=>'html'],['id'=>'\d+']);
静态路由 在conf目录route文件配置
<?php
return
# ext 为url后缀
['news/:name' => 'index/test/test',['ext'=>'html']]
;
apache2 配置文件在public .htaccess
/* vim /etc/apache2/apache2.conf */ <IfModule mod_rewrite.c>
RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.html)$ [NC]
RewriteRule ^/?(.*)$ /$1.html [QSA,R=301,L] RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L] #//第二种方法
#RewriteEngine on
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
</IfModule>
浏览器访问时 在phpstorm调试时要加index.php 例如下面的url要写成 https/localhost/index.php/news/6.html 不然无法访问。
生产环境不用加index.php
以下可以不用设置
1.修改apache2服务器的httpd.conf
var/www# vim /usr/local/apache2/conf/httpd.conf
确认加载了 mod_rewrite.so 模块(将如下配置前的 # 号去掉):
LoadModule rewrite_module modules/mod_rewrite.so
更改AllowOverride 配置
<Directory /var/www/tp5/public>
MultiViews
Options Indexes FollowSymLinks
AllowOverride FileInfo Options
#AllowOverride None Require all granted
</Directory>
最新文章
- 利用scp 远程上传下载文件/文件夹和ssh远程执行命令
- 网页端压缩解压缩插件JSZIP库的使用
- UDP发送数据测试
- json转换为javabean
- 搭建angularjs API文档站点
- 如何查询Oracle中用户所有信息
- 关于JavaScript的思考
- CSS文字大小单位px、em、pt(转)
- Access增删改查 (持续更新中)
- BZOJ3166: [Heoi2013]Alo
- app被Rejected 的各种原因翻译(转)
- ios创建画笔的样例(双笔画效果)
- unity3d 学习笔记(两)
- Java基础---Java---网络编程---TCP的传输、客户端和服务端的互访、建立一个文本转换器、编写一个聊天程序
- Nginx Install 记录
- 从Java小白到收获BAT等offer,分享我这两年的经验和感悟
- lnmp 搭建 初试
- 洛谷P2239 螺旋矩阵
- 一个源文件可以写出多个class吗?编译后,会不会生成多个class文件?
- Tom与Jerry谁先死?
热门文章
- linux让命令或程序在终端后台运行的方法(Ubuntu/Fedora/Centos等一样适用)
- windows下数据库备份bat
- pl/sql的安装使用详解
- Gym 100507H - Pair: normal and paranormal
- 阶段1 语言基础+高级_1-3-Java语言高级_05-异常与多线程_第3节 线程同步机制_1_线程安全问题的概述
- 基于3ds Max的游戏建模方案
- 【Unity Shader】---CG标准函数库
- vue登录注册实践
- 在VS Code中使用Jupyter Notebook
- 加密算法:DES、AES等