codeIgniter现代重写规则及控制器 [英] CodeIgniter Mod Rewrite Rules and the Controller

查看:161
本文介绍了codeIgniter现代重写规则及控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

学习PHP,我用的mod_rewrite和codeIgniter玩耍。我曾与正确的配置我的.htaccess文件

Learning PHP, I am playing around with mod_rewrite and CodeIgniter. I have configured my .htaccess file correctly with

RewriteEngine On
RewriteRule ^(resources)/(.*) $1/$2 [L]
RewriteRule ^(user_guide)/(.*) $1/$2 [L]
RewriteRule (.*) index.php?$1 [L]

我有点明白了正则表达式,并能AP preciate这里发生的事情。重写规则被应用,服务器,而不是处理在上面的个案重视的index.php最终URL(前端控制器)以pretty的URL。到目前为止,一切都很好。

I understand a bit of regex, and can appreciate what happens here. The rewrite rules are applied and the server than handles the final URL which in the above case- attaches index.php (the front controller) to the "pretty" URL. So far so good.

我现在想要一个URL模式:

I now want a URL pattern :

/<person-name>/at/<place>

要获得翻译为:

/index.php/person/list?personName=$1&place=$2

和我处理在人控制我的列表功能的要求。我不明白为什么下不工作:

And i handle the request at my list function in the person controller. I do not understand why the following doesn't work:

RewriteRule ^([a-z]+)/(at)/([a-z]+)$ index.php/person/list?personName=$1&place=$2 [L]

我究竟做错了什么/哪里是我的理解有缺陷?我看到占位符正确提取($ 1和$ 3),但是,它抛出一个codeIgniter 404。

What am i doing wrong/where is my understanding flawed? I see that the placeholders are extracted correctly ($1 and $3), however, it throws a CodeIgniter 404.

非常感谢!

推荐答案

这可能是最简单的修补程序将解决您的问题。通过包装的括号,你创建另外一个匹配的组,这意味着$ 2将永远是在。这可能是打破一切。你想的index.php人/列表PersonName的= $ 1安培;?地方= $ 3 但是,你可能已经注意到这个问题,并固定它不解决这个问题,在这种情况下,接着往下看。

It's possible that the simplest fix will fix your issue. By wrapping "at" in parentheses, you're creating another matching group, which means that $2 will always be "at." That could be breaking everything. You want index.php?person/list?personName=$1&place=$3 But you may have noticed that issue and fixed it without fixing the problem, in which case, read on.

查看<一个href="http://stackoverflow.com/questions/2894250/how-to-make-$c$cigniter-accept-query-string-urls">How使codeIgniter同意]查询字符串&QUOT;网址?。这似乎表明,你不能混搭段为基础的方法和查询字符串的方法。没有看到你的控制器code,我不能肯定地说,但我开始调查在那里。您也可以尝试:

Check out How to make CodeIgniter accept "query string" URLs?. It seems to indicate that you can't mix and match the segment-based approach and the query string approach. Without seeing your controller code, I can't say for certain, but I'd start investigating there. You might also try:

RewriteRule ^([a-z]+)/(at)/([a-z]+)$ index.php?person/list/$1/$3 [L]

这将做同样的事情下面的通用CI重定向规则确实;发送网址关到的index.php作为处理查询字符串。你说你得到了它与路线的工作,因此而不是传递查询字符串到控制器,你可以期待的人,地点和两个参数。查询字符串的CI的处理留下了很多不足之处,我从来没有试过mod_rewrite的东西,包括一个查询字符串转换成一个查询字符串。

which would do the same thing the general-purpose CI redirect rule below does; send the URL off to index.php as a query string for processing. You've said you got it working with routes, so rather than passing a query string to your controller, you can expect person and place as two arguments. CI's handling of query strings leaves a lot to be desired, and I've never tried to MOD_REWRITE something including a query string into a query string.

这篇关于codeIgniter现代重写规则及控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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