如何通过从codeigniter URL请求多个变量? [英] How to pass multiple variables from URL request in Codeigniter?

查看:130
本文介绍了如何通过从codeigniter URL请求多个变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关我的网站的每个用户,他们有一个文章列表自己的个人资料页面。当用户点击列表中的项目之一,动画会发生,然后AJAX调用会弹出一个显示的文章股利。与此同时,我用一些javascript强制URL变化来反映这一点:

For each user on my site, they have their own profile page with a list of articles. When the user clicks on one of the list items, an animation happens and then an AJAX call brings up a div that displays the article. At the same time, I use some javascript to force a URL change to reflect this:

http://www.example.com/UserName/Hello-World-Article

当用户点击后退按钮在浏览器中,它调用javascript函数动画回到列表视图状态。

When the user clicks the back button in their browser, it calls the javascript function to animate going back to the listview state.

到目前为止好。但是,假设用户输入上述网址的地址栏和presses进入,这里是问题:

So far so good. But let's say the user enters the above URL into their address bar and presses enter, here is the question:

我如何通过用户名和你好,世界第三的变量为codeigniter并正确使用它们在这方面?

How do I pass the 'UserName' and 'Hello-World-Article' variables into Codeigniter and use them properly in this context?

推荐答案

对不起,我有点困惑你的问题的措辞,但我想我明白你想谈什么。

Sorry, I was a little bit confused by the wording of your question, but I think I understand what you are trying to talk about.

第一:阅读的控制器文档 。 codeigniter文档的岩石。

First: Read the Docs on Controllers. Codeigniter documentation rocks.

二:的URI的被直接路由到控制器类名称及其功能

Second: The URI's are routed directly to controller class names and their functions.

例如:

<?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文件(文档使用的路线删除项目):

 $route['(:any)'] = 'profile/item/$1';

更好的办法,但(预读):

Better way though (read ahead):

 $route['profile/(:any)'] = 'profile/item/$1';

最后,这将创造你正在寻找的网址:

Finally, this will create the URL you are looking for:

http://www.example.com/username/whatever-i-want

//http://www.example.com/profile/username/whatever-i-want

我可能需要仔细检查该的语法错误,但它是如何codeigniter路由的基本工作原理。一旦您的网址是设置这样的,你可以做任何你想用你的JS。

I might need to double check this for syntax errors, but it's the basics of how Codeigniter routing works. Once your URL is setup like that, you can do whatever you want to it with your JS.

然而,我强烈反对这种方法,因为路由一类像这样将pretty的多渲染应用程序/网站的其余部分无用的,除非这是你唯一的控制器/功能(可能不是这样的),你有。我想你会更好只具有或URL拉上一个类名。

However, I strongly advise against this method because routing a class like this will pretty much render the rest of the application/site useless, unless this is your only controller/function (probably not the case) you have. I think you would be better off with just having a class name in the URL one way or another.

另外,你也可以只使用index()和$这个 - > URI的>段()一样,所以如果你想跳过以非传统的方式路由。

Alternatively, you could also just use index() and $this->uri->segment() like so if you want to skip the routing in a non-conventional way.

<?php

class Profile extends CI_Controller {

    function index()
    {
        $username = $this->uri->segment(1);
        $title = $this->uri->segement(2);
    }
}

希望,这是有道理的,并帮助你解决问题。

Hopefully, that makes sense and helps you with your problem.

这篇关于如何通过从codeigniter URL请求多个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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