CI路由问题
新人请教个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啊 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
另外,手册里的东西都是没问题的,这个不用怀疑。
Hex 发表于 2016-11-10 15:18
贴代码看看~
原则上手册写的都是没问题的。除非 CI 出了 BUG。
我贴个手册上面的那个
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Blogs extends CI_Controller {
public function index()
{
$this->load->view('welcome_message');
}
public function users()
{
echo ("url segment1: ");
echo ($this->uri->segment(1));
echo "<br><br><br><br>";
echo ("url segment2: ");
echo ($this->uri->segment(2));
echo "<br><br><br><br>";
echo ("url segment3: ");
echo ($this->uri->segment(3)); //用于某篇文章内容的ID
// echo aritle(segment(3)); //此处显示文章
echo "<br><br><br><br>";
$this->load->view('welcome_message');
}
}
设置路由 $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 好像不行吧?
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的内容,求解答。 $this->uri->rsegment(3) 接收路由后的值,仔细看看手册。 jackcott 发表于 2016-11-10 14:49
请教大神,
例子2是这种意思吧, 比如我设置了路由$route['blog/joe'] = 'blogs/users/34';
通过地址栏访 ...
没有显示内容,指的是什么呢? 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 jackcott 发表于 2016-11-10 15:10
程序出错, 比如blogs/users/34 显示的是一篇文章通过 http://127.0.0.1/index.php/blogs/users/34 正常显 ...
贴代码看看~
原则上手册写的都是没问题的。除非 CI 出了 BUG。 本帖最后由 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
可是实际上行不通啊,不知道怎么弄。 jackcott 发表于 2016-11-10 15:20
其实我是想设置成这种方式的,像这篇文章这种http://www.jamesstoddern.net/seo-friendly-urls-in-codeign ...
这个的 .html 是用的后缀。路由和后缀是两个不同的配置,你看看手册呀。
页:
[1]
2