zoukankan      html  css  js  c++  java
  • thinkPHP5.0 路由 前后端分离 绑定模块 隐藏入口文件

     1.前后端分离

    a、在网站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地址

    a、入口绑定模块之前

    http://www.tp5.com/入口文件/模块/控制器/操作

    b、入口绑定模块之后

    http://www.tp5.com/入口文件/控制器/操作

    3.隐藏入口文件

    1、开始apache 的重写   F:phpStudyPHPTutorialApacheconfhttpd.conf

    # 把注释去掉

    更改后:LoadModule rewrite_module modules/mod_rewrite.so

    2、设置访问权限    AllowOverride       none改为All

    3、入口文件 ,在网站public目录下新建.htaccess

    复制代码
    //phpstudy的第一种写法
    <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>
    复制代码
    复制代码
    //第一种方法不好使的话 使用第二种方法
    <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] </IfModule>
    复制代码

    4、重启服务 

    5、url地址变化

    a、隐藏之前

    http://www.tp5.com/index.php/Index/test

    b、隐藏之后

    http://www.tp5.com/Index/test

    tp5.0路由学习注意:路由的优点:1.简化url地址,方便记忆   2.有利于搜索引擎的优化

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

    1.支持三种方式的url解析规则

    2.路由只针对应用,不针对模块,因此路由的设置也是针对应用下面的所有模块。

    3.如果有些模块不想使用路由   关闭后台模块,在后台入口文件添加下面这句话,写在

    加载框架引导文件之后  否则报错
    // 加载框架引导文件 require __DIR__ . '/../thinkphp/start.php';
    //关闭admin模块下的路由 	hinkApp::route(false);

    4、路由模式

    a、普通模式

    1.关闭路由,完全使用默认的PATH_INFO方式URL

    2.形式:http://www.tp5.com/admin.php/Index/index

    3.如何配置:

    //是否开启路由

    ‘url_route_on’    => false,

    //是否强制使用路由

    'url_route_must'  => false,

    b、混合模式

    1.定义:开启路由,并使用路由定义+默认 PATH_INFO 方式的混合

    2.如何设置

     //是否开启路由

    ‘url_route_on’    => true,

    //是否强制使用路由

    'url_route_must'  => false,

    c、强制模式

    1.定义:

    开启路由,并设置必须定义路由才能访问

    2.如何设置

                 //是否开启路由

    ‘url_route_on’    => true,

    //是否强制使用路由

    'url_route_must'  =>true,

    5、设置路由-动态单个注册

    a、设置路由文件

    application oute.php

    b.、如何设置

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

             c、路由的形式

    1.静态地址路由

    // 注册路由test  访问到Index模块index控制器test方法

    Route::rule('test','index/Index/test');

    2.给路由带参数

    //注册带参数路由

    //http://www.tp5.com/course/1   路由模式

    //http://www.tp5.com/index/Index/course/id/1  普通模式

    Route::rule('course/:id','index/Index/course');

    //如果路由设置两个参数,必须带两个参数

    Route::rule('time/:year/:mouth','index/Index/shijian');

    3.可选参数路由

    //http://www.tp5.com/time/2017

    //http://www.tp5.com/time/2018/5

    Route::rule('time/:year/[:mouth]','index/Index/shijian');

    4、全动态路由(不建议使用)

                  Route::rule(':a/:b','index/Index/dongtai');

    5.完全匹配路由

                  //http://www.tp5.com/wanquan   可以访问
                  //http"//www.tp5.com/wanquan/ada/asf/a/f 不可以访问    
    
                  Route::rule('wanquan$','index/Index/wanquan');

    6.路由额外带参数

                  Route::rule('test1','index/Index/test1?id=12&name=adasfds');

    d、设置请求类型   

    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="" method="post">  post 重要
        <p>
            <input type="hidden" name="_method" value="PUT"> **隐藏域重要
            <input type="text" name="name">
        </p>
        <p>
            <input type="submit" value="提交">
        </p>
    </form>
    复制代码

    6、设置路由-动态批量注册

    a、基本格式

    Route::rule([ '路由规则1'=>'路由地址和参数',
    '路由规则2'=>['路由地址和参数','匹配参数(数组)','变量规则(数组)'] ... ],'','请求类型','匹配参数(数组)','变量规则');

    b、使用

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

    7、设置路由-配置文件批量注册

    return [ 'test'   =>  'index/Index/test',
        'course/:id'  =>  'index/Index/course' ];

    8、变量规则

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

    9、路由参数

    //路由参数method  请求方式必须是get
    //路由参数ext  主要设置路由的后缀
    Route::rule('course/:id','index/Index/course','get',['method'=>'get','ext'=>'html'],["id" => "d{1,3}"]);

     

     10、资源路由

    a、后台功能

    add页面、展示页面、删除功能、修改页面、修改功能、增加功能

           a、声明资源路由

    Route::resource('blog','index/Blog');

           b、会自动注册七个路由规则

    11、设置快捷路由

    a、声明

    Route::controller('user','index/User');

           b、使用:

    复制代码
    namespace appindexcontroller; class User { public function getInfo()
        {
        } public function getPhone()
        {
        } public function postInfo()
        {
        } public function putInfo()
        {
        } public function deleteInfo()
        {
        }
    }
    复制代码

           c、URL访问

    get http://localhost/user/info get http://localhost/user/phone post http://localhost/user/info put http://localhost/user/info delete http://localhost/user/info

    12、生成URL地址

    a、系统类

    use thinkurl

    url::build('index/Index/index');

    b、系统方法

    url('index/Index/index');

    c、使用

    看手册详细介绍

  • 相关阅读:
    Android和kernel杂散点集合
    Kernel的IIC驱动分析
    Uboot流程分析
    关于在eclipse当中,对接口方法的实现使用@Override报错的解决方式
    任务调度工具oozie和azkaban的对比
    实现Linux和windows之间实现文件传输的问题解决方式。sftp和ftp
    使用spring-boot创建定时任务。同时创建多线程执行定时任务。
    【C++11】新特性——auto的使用
    Inline函数使用注意事项
    结构体变量的sizeof计算
  • 原文地址:https://www.cnblogs.com/xiaogou/p/11660487.html
Copyright © 2011-2022 走看看