对不起,我对你的问题的措辞有点困惑,但我想我明白你在谈论什么。
第一张:阅读Docs on Controllers。 Codeigniter文档的岩石。
第二个: URI的路由直接控制器类名称及其功能。
例如:
<?php
class Profile extends CI_Controller {
function item($username = NULL, $title = NULL)
{
if (is_null($username) || is_null($title)) redirect(base_url());
// blah run code where $username and $title = URI segement
}
}
这将产生此网址:
http://www.example.com/profile/item/username/whatever-i-want
然后你可以在应用程序/配置/ routes.php文件使用路由删除项目(docs):
$route['(:any)'] = 'profile/item/$1';
虽然更好的方法(预读):
$route['profile/(:any)'] = 'profile/item/$1';
最后,这将创造你正在寻找的网址:
http://www.example.com/username/whatever-i-want
//http://www.example.com/profile/username/whatever-i-want
我可能需要仔细检查这个语法错误,但它的笨路由是如何工作的基本知识。一旦你的URL设置成这样,你就可以用JS来做任何你想做的事情。
然而,我强烈反对这种方法,因为路由一类像这样将几乎呈现应用程序/网站无用的其余部分,除非这是你唯一的控制器/功能(可能并非如此),你有。我认为只要在这种或那种URL中拥有一个类名就会更好。
或者,如果您想以非常规方式跳过路由,也可以像使用index()和$ this-> uri-> segment()一样使用。
<?php
class Profile extends CI_Controller {
function index()
{
$username = $this->uri->segment(1);
$title = $this->uri->segement(2);
}
}
希望这是有道理的,并帮助您解决您的问题。
谢谢!我正在使用替代方法。 – adrianmc 2013-05-11 12:29:17
真的很好的答案! – 2015-07-23 14:12:16