tp5学习笔记3

1、路由作用:
1、简化URL地址,方便大家记忆

2、有利于搜索引擎优化

2、入口文件:

1、前后台分离

    a、在网站public目录下(/var/www/tp5/public) 新建admin.php

    b、打开admin.php
        <?php
        // 定义应用目录
        define('APP_PATH', __DIR__ . '/../application/');
        // 加载框架引导文件
        require __DIR__ . '/../thinkphp/start.php';

2、绑定模块
    1、实现功能
        index.php 这个入口文件 只能去前台模块
        admin.php 这个入口文件 只能去后台模块 #建议后台的入口文件稍微复杂一些

    2、如何实现
        在入口文件中

        define("BIND_MODULE",'index'); # 绑定前台模块
        define("BIND_MODULE",'admin'); # 绑定后台模块

    3、URL地址发生改变
        1、入口绑定之前
            http://localhost/admin.php/模块/控制器/方法
        2、入口绑定之后
            http://localhost/admin.php/控制器/方法

3、隐藏入口文件
    nginx放入location/{}里面
        if (!-e $request_filename) {
           rewrite  ^(.*)$  /index.php?s=/$1  last;
           break;
        }

    重启服务

    5、url地址变化
        1、隐藏之前
            http://localhost/index.php/Index/test
        2、隐藏之后
            http://localhost/Index/test

3、Tp5.0路由学习注意:
1、支持三种方式的URL解析规则
2、路由只针对应用,不针对模块,因此路由的设置也是针对应用下面的所有模块。
3、关闭后台模块,在后台入口文件(/var/www/tp5/public)

// 定义应用目录
define('APP_PATH', __DIR__ . '/../application/');
// 绑定后台
define('BIND_MODULE','admin');
// 加载框架引导文件
require __DIR__ . '/../thinkphp/start.php';
// 关闭admin模块的路由
// 必须写到 加载框架引导文件 之后否则报错
\think\App::route(false);

4、路由模式

1、普通模式
    a、定义
        关闭路由,完全使用默认的 PATH_INFO 方式URL:

    b、形式
        http://localhost/admin.php/index/index

    c、如何设置
        // 是否开启路由
        'url_route_on'           => false,
        // 是否强制使用路由
        'url_route_must'         => false,

2、混合模式
    a、定义:
        开启路由,并使用路由定义+默认 PATH_INFO 方式的混合

    b、如何设置
        // 是否开启路由
        'url_route_on'           => true,
        // 是否强制使用路由
        'url_route_must'         => false,

3、强制模式
    1、定义:
        开启路由,并设置必须定义路由才能访问

    2、如何设置
        // 是否开启路由
        'url_route_on'           => true,
        // 是否强制使用路由
        'url_route_must'         => true,

5、设置路由-动态单个注册
0、设置路由格式
Route::rule(‘路由表达式’,’路由地址’,’请求类型’,’路由参数(数组)’,’变量规则(数组)’)
1、设置路由文件
/var/www/tp5/application/route.php

2、如何设置

    // 引入系统类
    use think\Route;
    // 定义路由规则
    // 设置路由之后,就不能使用pathinfo访问了
    // 注册路由 访问到Index模块index控制器index方法
    Route::rule('/','index/index/index');
    // 注册路由test 访问到Index模块index控制器test方法
    Route::rule('test','index/index/test');

3、路由的形式
    1、静态地址路由
        // 注册路由test 访问到Index模块index控制器test方法
        Route::rule('test','index/index/test');

    2、路由带参数
        // 注册带参数路由
        // http://localhost/couser/1
        // http://localhost/index/index/index/id/1
        Route::rule('course/:id','index/index/course');
        // 如果路由设置两个参数,不许带两个参数
        Route::rule('time/:year/:month','index/index/shijian');

    3、可选参数路由
        // http://localhost/time/2017
        // http://localhost/time/2017/8
        Route::rule('time/:year/[:month]','index/index/shijian');

    4、全动态路由(不建议大家使用)
        Route::rule(':a/:b','index/index/dongtai');

    5、完全匹配路由
        // http://localhost/test1 #可以成功访问
        // http://localhost/test1/1 #不能访问
        Route::rule('test1$','Index/index/test1');

    6、路由额外带参数
        Route::rule('test2','Index/index/test2?id=10&name=zhangsan');

4、设置请求类型
    1、TP中请求类型
        get、post、put、delete

    2、Route::rule() 默认支持所有请求类型

    3、设置各种请求
        // 支持get请求
            Route::rule('type','Index/index/type','get');
            // Route::get('type','Index/index/type');

        // 支持post请求
            // Route::rule('type','Index/index/type','post');
            // Route::post('type','Index/index/type');

        // 同时支持get和post
            // Route::rule('type','Index/index/type','get|post');

        // 支持所有路由
            // Route::rule('type','Index/index/type','*');
            // Route::any('type','Index/index/type');

        // 支持put请求
            Route::rule('type','Index/index/type','put');
            Route::put('type','Index/index/type');

        // 支持delete请求
            Route::rule('type','Index/index/type','delete');
            Route::delete('type','Index/index/type');

    4、如何模拟put和delete请求
        <form action="type" method="post">**
            <p>
                <input type="hidden" name="_method" value="PUT">**
                <input type="text" name="name" id="">
            </p>
            <p>
                <input type="submit" value="提交">
            </p>
        </form>

6、设置路由-动态批量注册
1、基本格式
Route::rule([
‘路由规则1’=>’路由地址和参数’,
‘路由规则2’=>[‘路由地址和参数’,’匹配参数(数组)’,’变量规则(数组)’]
],’’,’请求类型’,’匹配参数(数组)’,’变量规则’);

2、使用
    Route::rule([
        "test"=>"index/index/test",
        "course/:id"=>"index/index/course"
    ],'','get');

    Route::get([
        "test"=>"index/index/test",
        "course/:id"=>"index/index/course"
    ]);

7、设置路由-配置文件批量注册
return [
“test”=>”index/index/test”,
“course/:id”=>”index/index/course”
];

8、变量规则
// Route::rule(‘路由表达式’,’路由地址’,’请求类型’,’路由参数(数组)’,’变量规则(数组)’);

// 设置路由参数id必须是数字,必须1-3位
Route::rule("course/:id","index/index/course",'get',[],['id'=>'\d{1,3}']);

9、路由参数
// Route::rule(‘路由表达式’,’路由地址’,’请求类型’,’路由参数(数组)’,’变量规则(数组)’);
Route::rule(“course/:id”,”index/index/course”,’get’,[‘method’=>’get’,’ext’=>’html’],[‘id’=>’\d{1,3}’]);
// 路由参数method 请求方式必须是get
// 路由参数ext 主要设置路由的后缀

10、资源路由
1、声明
Route::resource(‘blog’,’index/blog’);

2、会自动注册七个路由规则
    get     blog                  index   # 后台展示
    get     blog/create      create  # 添加页面
    post    blog               save    # 增加操作
    get     blog/:id         read    
    get     blog/:id/edit    edit    # 修改页面
    put     blog/:id         update  # 更新操作
    delete  blog/:id         delete  # 删除操作

11、设置快捷路由

1、声明
    Route::Controller('blog','index/blog');


2、控制器中
    namespace app\index\controller;

    class Blog{
        public function  getindex(){
            echo "我是bolg控制器index方法";
        }

        public function geta(){
            echo "AAAAAAAA";
        }
    }

3、URL访问
    http://localhost/blog/a
    http://localhost/blog/index

12、生成url地址
1、系统类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
	dump(Url::build('index/index/index'));

2、系统方法

dump(url('index/index/index'));

3、使用
// 普通url地址
dump(Url::build('index/index/index'));
dump(url('index/index/index'));

// 带参数url
dump(url('index/index/abc',['id'=>10,'name'=>"张三"]));
dump(url('index/index/abc','id=10&name=100'));
// string(45) "/index/abc/id/10/name/%E5%BC%A0%E4%B8%89.html"
// string(30) "/index/abc/id/10/name/100.html"

// 带锚点
dump(url('index/index/abc#name',['id'=>10,'name'=>"100"]));
// string(35) "/index/abc/id/10/name/100.html#name"

// 带域名
dump(url('index/index/abc#name@blog',['id'=>10,'name'=>"100"]));
// string(53) "http://blog.tp.com/index/abc/id/10/name/100.html#name"

// 加入口文件
Url::root('/index.php');
dump(url('index/index/abc#name@blog',['id'=>10,'name'=>"100"]));
// string(63) "http://blog.tp.com/index.php/index/abc/id/10/name/100.html#name"
  • thinkCMF上传图片

    thinkCMF上传图片 1、要使用这个功能,首先要引入框架自带的admin.js或者frontend.js文件 再上传的地方添加 1234567891011121314151617181920212223242526...

    thinkCMF上传图片
  • 小程序开发

    小程序开发 获取access_token要将申请的测试号的appid和secret填在相应的位置,而不是填公众号的appid和secret。

    小程序开发
  • global与GLOBAL区别

    global与GLOBAL区别 1.有些场合需要全局变量的出现,如下例子:<?php$name=”why”;//定义变量name,并初始化function echoName(){//试图引用函数外面的变量echo “m...

    global与GLOBAL区别
  • PHP取整

    PHP取整 1.直接取整,舍弃小数,保留整数:intval($a);2.四舍五入取整:round($a);3.向上取整,有小数就加1:ceil($a);4.向下取整:floor($a)。

    PHP取整
  • 为什么PHP变量以$开头

    PHP变量 ​ Because PHP was based on Perl which used $, though the symbols Perl used were meaningful andplenty us...

    为什么PHP变量以$开头
  • PHP超全局变量

    PHP超全局变量 PHP中预定义了几个超级全局变量(superglobals) ,这意味着它们在一个脚本的全部作用域中都可用。 你不需要特别说明,就可以在函数及类中使用。 PHP 超级全局变量列表: $GLOBALS $G...

    PHP超全局变量
  • 面试准备

    PHP面试题目 1、阿里PHP面试​ https://blog.csdn.net/qishouzhang/article/details/47007177 1、get与post的区别​ 最直观的区别就是GET把参数...

    面试准备
  • tp5学习笔记9

    tp5学习笔记9 1、验证器1) 控制器中使用验证器 // 实例化验证器类 $validate=new Validate( [ &q...

    tp5学习笔记9
  • tp5学习笔记8

    tp5学习笔记8 1、视图a) 加载页面 1、继承系统控制器类 return $this->fetch(参数1,参数2,参数3,参数4); 参数1(字符串): 模板...

    tp5学习笔记8
  • tp5学习笔记7

    tp5学习笔记7 1、模型(model)数据模型 2、新建数据模型​ 1) 手动新建 在model目录下 新建 User.php // 声明命名空间 namespace a...

    tp5学习笔记7