zoukankan      html  css  js  c++  java
  • tp6_004路由配置

     

    1、闭包定义参数

    Route::get('hello/:name', function($name) {
    return 'Hello,' . $name;
    });
     
     

    2、最简单的路由 闭包路由

    Route::get('hello', function() {
    return 'Hello,ThinkPHP';
    });
     

    3、路由到控制器方法 并传参 name

    Route::get('tr001/:name', 'Troute/t001');
     
     

    4、路由的优先级,把复杂的路由放到前面,不然永远是匹配简单的路由

     
    路由并非完整匹配,所以如果你定义了下面的路由
     
    Route::get('hello', 'Hello/index');
    Route::get('hello/:name', 'Index/hello');
     
    如果访问的URL是
    永远只会匹配到第一条路由。办法是把路由规则复杂的放到前面
     
    Route::get('hello/:name', 'Index/hello');
    Route::get('hello', 'Hello/index');
     
    也可以定义一个完整匹配
     
    Route::get('hello$', 'Hello/index');
    Route::get('hello/:name$', 'Index/hello');
     

    5、路由路由注册也可以带上匹配条件检查

    Route::get('hello/:name', 'Index/hello')
    ->ext('html')
    ->https();
     
    表示只有当访问才会匹配该路由
     
     

    6、可选参数

    Route::get('tr002/[:name]', 'Troute/t002');
     
    public function t002($name='')
    {
    return 'hello this is Troute class t002 action ,name = '.$name;
    }
     

    7、路由的请求类型限制

     
    Route::get('blog/:id','Blog/read');
    Route::post('blog', 'Blog/save');
    Route::put('blog/:id', 'Blog/update');
    Route::delete('blog/:id', 'Blog/delete');
    Route::patch('blog/:id','Blog/patch');
    Route::options('blog','Blog/info');
     
    你可以注册一个可以接受任何请求类型的路由
    Route::any('hello','Index/hello');
     
    或者注册一个多个请求类型的路由
    Route::rule('hello','Index/hello', 'get|post');
     

    8、重定向路由

     
    Route::redirect('tr003', 'https://www.kancloud.cn/thinkphp/thinkphp6-quickstart/1352495', 301);
     

    9、路由到模板

     
    Route::view('tr004', 'index/tr004');
     

    10、如果不需要路由

    可以在该应用下的app.php配置文件中设置
     
    'with_route' => false,
     

    11、强制路由

     
    // 强制使用路由
    'url_route_must' => true,
     

    12、路由分组

     
    对路由进行分组可以有效提高路由的匹配性能
     
    Route::group('troute', function() {
    Route::get('/tr005', 'Troute/t005');
    Route::get('/tr006', 'Troute/t006');
    })->ext('html')->pattern(['id' => 'd+']);
     
    访问地址如下:
     
     
    虚拟分组
     
    Route::group(function() {
    Route::get('blog/:id', 'Blog/read');
    Route::get('user/:name', 'User/read');
    })->ext('html')->pattern(['id' => 'd+']);
     

    13、url生成

     
    使用Route类
    Route::buildUrl('index/hello', ['name' => 'thinkphp']);
     
     
    助手函数
    url('index/hello', ['name' => 'thinkphp']);
     
    生成url不含域名
    url('index/hello', ['name' => 'thinkphp' , 'extra' => 'test']);
     
    结果为:/index/index/hello.html?name=thinkphp&extra=test
     
     
    生成url含域名
    url('index/hello', ['name' => 'thinkphp' , 'extra' => 'test'])->domain(true);
     
    结果为:http://tp6.cn/index/index/hello.html?name=thinkphp&extra=test
     

    但行好事,莫问前程!

    本文来自博客园,作者:yangphp,转载请注明原文链接:https://www.cnblogs.com/ypeih/p/15390965.html

  • 相关阅读:
    函数指针
    动态内存
    char*和char[]的区别
    C语言基本数据类型大小
    html5新特性localStorage和sessionStorage
    Swoole实现h5版聊天室笔记
    php使用mysql之sql注入(功)
    Http协议工作特点和工作原理笔记
    原生js使用ajax
    php常用的几个预定义变量
  • 原文地址:https://www.cnblogs.com/ypeih/p/15390965.html
Copyright © 2011-2022 走看看