域名路由

  1. 之前所有的 URL,都是手动键入的,而路由也提供了一套生成方法;
    Url::build(‘地址表达式’,[‘参数’],[‘URL 后缀’],[‘域名’])
    url(‘地址表达式’,[‘参数’],[‘URL 后缀’],[‘域名’])
    2. 在 Collect 演示生成,拿 Blog 来实现 URL 地址;
    3. 使用 build()方法,只传一个控制器时,会被误认为 Collect 下的 blog 方法;
    1. Url::build('Blog');
    2. // /collect/blog.html
  2. 在没有设置路由的情况下,传递一个控制器以及操作方法;
    1. Url::build('Blog/create');
    2. // /blog/create.html
  3. 如果设置了对应路由,第 4 条生成的 URL 会相应的改变;
    1. Route::get('bc', 'Blog/create');
    2. // /bc.html
    3. Route::get('bl/cr', 'Blog/create');
    4. // /bl/cr.html
  4. 下面是没有设置路由和设置路由的带参数的 URL 生成;
    1. Url::build('Blog/read', 'id=5');
    2. // /blog/read/id/5.html
    3. // /read/5.html
  5. 参数部分,也可以用数组的方式,当然,多参数也支持;
    1. Url::build('Blog/read', ['id'=>5]);
    2. Url::build('Blog/read', 'id=5&uid=10');
    3. Url::build('Blog/read', ['id'=>5, 'uid'=>10]);
  6. 也可以使用助手函数 url 直接来设置;
    1. url('Blog/read', ['id'=>5]);
  7. 也可以使用普通的地址来设置 url;
    1. Url::build('Blog/read?id=5');
  8. 也可以使用和路由规则配对的方式设置 url;
    1. Url::build('/read/5');
  9. 在 app.php 可以设置默认 html 后缀,也可以在方法第三个参数设置;
    1. url('Blog/edit', ['id'=>5], 'shtml');
  10. 使用#name,可以生成一个带锚点的 url;
    1. url('Blog/edit#name', ['id'=>5]);
  11. 使用 Url::root(‘/index.php’)在 URL 前面加上一个 index.php;
    14. 但这个添加需要整体考虑路径是否支持或正确,否则无法访问;
    15. 在本身有 index.php 的时候,使用 Url::root(‘/‘)隐藏;
    1. Url::root('/index.php');