用户
 找回密码
 入住 CI 中国社区
搜索
查看: 3021|回复: 13
收起左侧

[已解决] CI路由问题

[复制链接]
发表于 2016-11-10 12:09:46 | 显示全部楼层 |阅读模式
新人请教个CI路由的问题,看了手册中的例子:
例子1:
$route['journals'] = 'blogs';
URL 的第一段是单词 "journals" 时,将重定向到 "blogs" 类。
例子2:
$route['blog/joe'] = 'blogs/users/34';
URL 包含 blog/joe 的话,将重定向到 "blogs" 类和 "users" 方法。ID 参数设为 "34" 。

照着做了, 第一个例子没问题,为何第二个例子接收不到参数34呢?
访问blog/joe用定向到blogs/users/ 用了 $this->uri->segment(3) 得到的是个空值,接收不到参数34啊
发表于 2016-11-10 15:38:06 | 显示全部楼层
jackcott 发表于 2016-11-10 15:35
我贴个手册上面的那个

设置路由 $route['blog/joe'] = 'blogs/users/34';

用 segment(3) 取不出来,要用 rsegment(3) 你看看多一个字母r,这个手册有一个章节叫 URI 类里面写了。参考 http://codeigniter.org.cn/user_guide/libraries/uri.html
另外,手册里的东西都是没问题的,这个不用怀疑。
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2016-11-10 15:35:41 | 显示全部楼层
Hex 发表于 2016-11-10 15:18
贴代码看看~

原则上手册写的都是没问题的。除非 CI 出了 BUG。

我贴个手册上面的那个
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');

  3. class Blogs extends CI_Controller {

  4.              public function index()
  5.         {
  6.                 $this->load->view('welcome_message');
  7.         }
  8.          
  9.          
  10.          
  11.          
  12.         public function users()
  13.         {       
  14.        
  15.                 echo ("url segment1: ");
  16.                 echo ($this->uri->segment(1));
  17.                 echo "<br><br><br><br>";
  18.                 echo ("url segment2: ");
  19.                 echo ($this->uri->segment(2));
  20.                 echo "<br><br><br><br>";
  21.                 echo ("url segment3: ");
  22.                 echo ($this->uri->segment(3)); //用于某篇文章内容的ID
  23.                 // echo aritle(segment(3)); //此处显示文章
  24.                 echo "<br><br><br><br>";
  25.                 $this->load->view('welcome_message');
  26.         }
  27.        

  28. }
复制代码

设置路由 $route['blog/joe'] = 'blogs/users/34';
按照手册上面的理解访问http;//127.0.0.1/index.php/blog/joe 等价于访问=>127.0.0.1/index.php/blog/user/34
程序通过获取segment(3)=>34  用于显示文章, 可实际上这个segment(3) 是个空值,导致无法显示。
所以 http;//127.0.0.1/index.php/blog/joe =>127.0.0.1/index.php/blog/user/34 好像不行吧?
 楼主| 发表于 2016-11-10 14:49:19 | 显示全部楼层
Hex 发表于 2016-11-10 14:10
$this->uri->rsegment(3) 接收路由后的值,仔细看看手册。

请教大神,
例子2是这种意思吧, 比如我设置了路由$route['blog/joe'] = 'blogs/users/34';
通过地址栏访问http://127.0.0.1/index.php/blog/joe, 通过路由解析访问的就是http://127.0.0.1/index.php/blogs/users/34(这个页面存在)
可是我做了这个实验,实际访问出错,通过访问http://127.0.0.1/index.php/blog/joe 并不能显示
http://127.0.0.1/index.php/blogs/users/34的内容,求解答。
发表于 2016-11-10 14:10:23 | 显示全部楼层
$this->uri->rsegment(3) 接收路由后的值,仔细看看手册。
发表于 2016-11-10 15:01:27 | 显示全部楼层
jackcott 发表于 2016-11-10 14:49
请教大神,
例子2是这种意思吧, 比如我设置了路由$route['blog/joe'] = 'blogs/users/34';
通过地址栏访 ...

没有显示内容,指的是什么呢?
 楼主| 发表于 2016-11-10 15:10:44 | 显示全部楼层
Hex 发表于 2016-11-10 15:01
没有显示内容,指的是什么呢?

程序出错, 比如blogs/users/34 显示的是一篇文章通过 http://127.0.0.1/index.php/blogs/users/34 正常显示。
但是访问http://127.0.0.1/index.php/blog/joe 并不能显示文章。
好像缺少了个参数34
发表于 2016-11-10 15:18:21 | 显示全部楼层
jackcott 发表于 2016-11-10 15:10
程序出错, 比如blogs/users/34 显示的是一篇文章通过 http://127.0.0.1/index.php/blogs/users/34 正常显 ...

贴代码看看~

原则上手册写的都是没问题的。除非 CI 出了 BUG。
 楼主| 发表于 2016-11-10 15:20:19 | 显示全部楼层
本帖最后由 jackcott 于 2016-11-10 15:21 编辑
Hex 发表于 2016-11-10 15:01
没有显示内容,指的是什么呢?

其实我是想设置成这种方式的,像这篇文章这种http://www.jamesstoddern.net/seo-friendly-urls-in-codeigniter-the-challenge.html这种
通过设置路由
$route['title.html']="blog/article/1";

通过访问xxx.com/title.html ------>实际访问 xxx.com/blog/article/1

可是实际上行不通啊,不知道怎么弄。
发表于 2016-11-10 15:34:30 | 显示全部楼层
jackcott 发表于 2016-11-10 15:20
其实我是想设置成这种方式的,像这篇文章这种http://www.jamesstoddern.net/seo-friendly-urls-in-codeign ...

这个的 .html 是用的后缀。路由和后缀是两个不同的配置,你看看手册呀。

本版积分规则