CodeIgniter-动态URL段 [英] CodeIgniter - Dynamic URL segments

查看:46
本文介绍了CodeIgniter-动态URL段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人可以帮助我.

I was wondering if someone could help me out.

我在我的codeigniter应用程序中建立了一个论坛,但在弄清楚如何构建细分时遇到了一些麻烦.

Im building a forum into my codeigniter application and im having a little trouble figuring out how i build the segments.

根据CI用户指南,uri的构建方式如下

As per the CI userguide the uri is built as follows

www.application.com/CLASS/METHOD/ARGUMENTS

这很好,除了我需要在结构上有所不同.

This is fine except i need to structure that part a bit different.

在我的论坛中,我有类别和帖子,因此要查看类别,请使用以下网址

In my forum i have categories and posts, so to view a category the following url is used

www.application.com/forums

这很好,因为它是类名,但是我想让下一段动态化,例如,如果我有一个名为"mycategory"的类别和一个名为"this-is-my-first-post"的帖子',则结构应为

This is fine as its the class name, but i want to have the next segment dynamic, for instance if i have a category called 'mycategory' and a post by the name of 'this-is-my-first-post', then the structure SHOULD be

www.application.com/forums/mycategory/this-is-my-first-post

我似乎无法实现这一点,因为根据文档,"mycategory"必须是一种方法,即使我要执行/forums/category/mycategory/this-is-my-first-post之类的操作,变得令人困惑.

I cant seem to achieve that because as per the documentation the 'mycategory' needs to be a method, even if i was to do something like /forums/category/mycategory/this-is-my-first-post it still gets confusing.

如果有人以前曾经做过这样的事情,请给我一点点启示,我对此很执着.

If anyone has ever done something like this before, could they shed a little light on it for me please, im quite stuck on this.

干杯

推荐答案

文档中没有什么让人困惑的地方,但是您有点困惑.让我给你一些建议.
您创建一个视图,在该视图中创建要单击的超链接,并在该超链接中提供此说明

Nothing is confusing in the document but you are a little bit confused. Let me give you some suggestions.
You create a view where you create hyperlinks to be clicked and in the hyperlink you provide this instruction

<a href="www.application.com/forums/category/mycategory/this-is-my-first-post">First Post</a>

在控制器中,您可以轻松获得此

In the controller you can easily get this

$category = $this->uri->segment(3);
$post     = $this->uri->segment(4);

现在您可以继续.如果您认为您的要求还有其他问题,可以使用黑客工具,我为此创建了一种动态分配细分的方法.

And now you can proceed. If you think your requirements are something else you can use a hack i have created a method for this which dynamically assign segments.

转到system/core/uri.php并添加此方法

Go to system/core/uri.php and add this method

function assing_segment($n,$num)
{
    $this->segments[$n] =   $num;
    return $this->segments[$n];
}

使用方法

$this->uri->assign_segment(3,'mycategory');
$this->uri->assign_segment(4,'this-is-my-first-post');

如果您遇到错误您提交的uri不允许使用字符",请转到application/config/config.php并在其中添加-

And if you have error 'The uri you submitted has disallowed characters' then go to application/config/config.php and add - to this

$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-';

这篇关于CodeIgniter-动态URL段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆