《ThinkPHP 5.0快速入门》 基础和路由
一、基础:
创建项目:conposer create-project topthink/think tp5 --prefer-dist
创建项目模块:php think build --module demo
访问未配置的路由:http://localhost/tp5/
上线时要关闭调试模式:'app_debug' => false, config.php
//创建母案文件需要继承controller类
use think\Controller; class Index extends Controller($name = '张三'){
public function index($name = '张三'){
$this->assgin('name',$name);//可在html输出{$name}为张三
return $this->fetch();
}
}
//创建数据表 -- 记录没试过
CREATE TABLE IF NOT EXISTS `think_data`(
`id` int(8) unsigned NOT NULL AUTO_INCREMENT,
`data` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;INSERT INTO `think_data`(`id`,`data`) VALUES
(1,'thinkphp'),
(2,'php'),
(3,'framework');
//连接数据库
use think\Db; class Index extends Controller{
public function index(){
$data = Db::name('data')->find();
$this->assign('result', $data);
return $this->fetch();
}
}
二、URL和路由:
1、url访问:
标准的URL访问格式:http://serverName/index.php/模块/控制器/操作/参数名/参数/参数名2/参数2
传入参数还可以使用:http://serverName/index.php/模块/控制器/操作?参数名=参数&参数名2=参数2
//提示:模块在ThinkPHP中的概念其实就是应用目录下面的子目录,而官方的规范是目录名小写,因此模块全部采用小写命名,无论URL是否开启大小写转换,模块名都会强制小写。
//如果你的控制器是驼峰的,那么访问的路径应为:.../hello_world/index
class HelloWorld{
public function index(){}
}
//如果使用.../HelloWorld/index 则会报错:Helloworld不存在。必须要使用大小写的时候需要配置:
关闭URL自动转换(支持驼峰访问控制器):'url_convert' => false, config.php
2、隐藏index.html
//隐藏路径中的index.php,需要在入口文件平级的目录下添加.htaccess文件(默认包含该文件)
<IfModule mod_rewrite.c>
Options +FollowSymlinks -Multiviews
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
</IfModule>
//接下来就可以使用:http://tp5.com/index/index/hello 访问了
//如果使用的是Nginx环境:可以在Nginx.conf中添加:
localtion/{
if(!-e$request_filename){
rewrite ^(.*)$ /index.php?s=/$1 last;
break;
}
}
3、*定义路由:
//路由定义文件:application/route.php 原来的url将会失效!变为非法请求!
return ['hello/:name'=>'index/index/hello',];//此方法的 name参数必选!
return ['hello/[:name]'=>'index/index/hello'];//此方法的 name参数可选!
//动态定义路由:application/route.php
use think\Route; Rroute::rule('hello/:name/:age','index/hello');//必选参数
Rroute::rule('hello/[:name]/[:age]','index/hello');//可选参数
4、路由参数
//可以用来约束URL后缀条件等。.../hello/index.html有效
return [
'hello/[:name]' => ['index/hello', ['method' => 'get', 'ext' => 'html']],
];
5、变量规则
//可以用正则表达式来限制路由参数的格式
return [
'blog/:year/:month' => ['blog/archive', ['method' => 'get'], ['year' => '\d{4}', 'month' => '\d{2}']],
'blog/:id' => ['blog/get', ['method' => 'get'], ['id' => '\d+']],
'blog/:name' => ['blog/read', ['method' => 'get'], ['name' => '\w+']],
];
6、路由分组
return [
'[blog]' => [
':year/:month' => ['blog/archive', ['method' => 'get'], ['year' => '\d{4}', 'month' => '\d{2}']], //blog/archive
':id' => ['blog/get', ['method' => 'get'], ['id' => '\d+']], //blog/get
':name' => ['blog/read', ['method' => 'get'], ['name' => '\w+']], //blog/read
],
];
《ThinkPHP 5.0快速入门》 基础和路由的更多相关文章
- 《ThinkPHP 5.0快速入门》 请求和响应
1.请求对象 //传统调用$request = Request::instance();//实例化对象 $request->url();//获取当前的域名 //继承think\Controlle ...
- 《ThinkPHP 5.0快速入门》 数据库、查询语言
1.数据库配置 return [ 'type' => 'mysql',// 数据库类型 'hostname' => '127.0.0.1',// 服务器地址 'database' => ...
- 快速入门系列--MVC--02路由
现在补上URL路由的学习,至于蒋老师自建的MVC小引擎和相关案例就放在论文提交后再实践咯.通过ASP.NET的路由系统,可以完成请求URL与物理文件的分离,其优点是:灵活性.可读性.SEO优化.接下来 ...
- Scala快速入门 - 基础语法篇
本篇文章首发于头条号Scala快速入门 - 基础语法篇,欢迎关注我的头条号和微信公众号"大数据技术和人工智能"(微信搜索bigdata_ai_tech)获取更多干货,也欢迎关注我的 ...
- python3.5+django2.0快速入门(一)
因为这篇教程需要用到anaconda的一些操作,如果还不懂anaconda的操作的同学可以看下这篇文章python 入门学习之anaconda篇. 创建python3+的开发环境 直接在终端输入:co ...
- ExtJs 6.0+快速入门,ext-bootstrap.js文件的分析,各版本API下载
ExtJS6.0+快速入门+API下载地址 ExtAPI 下载地址如下,包含各个版本 http://docs.sencha.com/misc/guides/offline_docs.html 1.使用 ...
- python3.5+django2.0快速入门(二)
昨天写了python3.5+django2.0快速入门(一)今天将讲解配置数据库,创建模型,还有admin的后台管理. 配置数据库 我们打开mysite/mysite/settings.py这个文件. ...
- TensorFlow 2.0 快速入门指南 | iBooker·ApacheCN
原文:TensorFlow 2.0 Quick Start Guide 协议:CC BY-NC-SA 4.0 自豪地采用谷歌翻译 不要担心自己的形象,只关心如何实现目标.--<原则>,生活 ...
- Thinkphp5.0快速入门笔记(2)
学习来源与说明 https://www.kancloud.cn/thinkphp/thinkphp5_quickstart 测试与部署均在windows10下进行学习. 示例建立新的模块和控制器 在a ...
随机推荐
- Elastic-Job开发指南(转)
原文地址:http://dangdangdotcom.github.io/elastic-job/post/1.x/user_guide/ 开发指南 代码开发 作业类型 目前提供3种作业类型,分别是S ...
- python第三方库安装
如安装jieba分词库 代码对Python 2/3均兼容 全自动安装:easy_install jieba或者pip install jieba / pip3 install jieba 半自动安装: ...
- Window Service安装不成功
1. 加Winsow Service 2. 加Setup Project Add -> Project Output , 选中Primary output from Winsow Serv ...
- 请求返回模板定制,@RestControllerAdvice
- c/c++读取一行可以包含空格的字符串(getline,fgets用法)
1.char[]型 char buf[1000005]; cin.getline(buf,sizeof(buf)); 多行文件输入的情况: while(cin.getline(buf,sizeof(b ...
- *p++=i怎么理解?
#include<stdio.h> void fibonacci(int *p,int n) { *p++=1; *p++=1; while(n>2) { *p++=*(p-1)+* ...
- libpng warning: iCCP: known incorrect sRGB profile告警处理
在 qt中加载某些 png图片会出现:libpng warning: iCCP: known incorrect sRGB profile 告警信息. 虽然没什么影响,但是总看到这个警告非常的不舒 ...
- nginx实现动静分离的负载均衡集群
实战: 一.源码编译安装nginx [root@tiandong63 ~]#yum groupinstall "Development Tools" "Developme ...
- nginx部署前端项目
1.在阿里云服务器上安装nginx,推荐使用yum安装 yum install -y nginx // 命令安装 nginx 服务器 2.配置nginx 安装完成后,进入 nginx 配置文件目录 一 ...
- PHP学习之PHP代码的优化
if代码块的优化 if(1===$orderState){ $status='success'; }else{ $status='error'; } return $status; 简 ...