在 CodeIgniter 中的控制器 URI 段之前传递变量
我想在 URL 中的控制器段之前传递一些站点范围内经过验证的变量。
示例:
默认 URL 为:
www.mysite.com/controller/method/variable/
有时我还希望有这样的 URL 来引用用户创建的此站点的子配置(主题、菜单等),以便用户可以很好地共享此站点 URL,其他人也可以通过他的自定义配置查看该网站。
www.mysite.com/username/controller/method/variable
这里用户名是base_url的自定义部分。它应该针对数据库进行验证,并将其设置为会话变量,以便稍后在我的控制器中使用它并更改主题。此外,在使用此用户名进入网站后,该网站上的所有链接都将开始使用www.mysite.com/username
作为base_url网址。
解决这个问题的一种方法是像这样路由:
controller/method/variable_name1/variable_value1/user_conf/username
...并将实现添加到我的项目中的每个控制器中。但这不是一个优雅的解决方案。
I would like to pass some site-wide validated variables before controller segment in URL.
Example:
Default URL would be:
www.mysite.com/controller/method/variable/
Sometimes I'd like to have also URL like this to refer to user created sub-configuration of this site (theme, menus, ...), so user could share this site URL nicely and others would see the site though his custom configurations.
www.mysite.com/username/controller/method/variable
Here username is custom part of base_url. It should be validated against database and set as session variable to use it later in my controllers and change theme for example. Also all the links on the site would start to use www.mysite.com/username
as base_url after website is entered with this username in the URL.
One way to solve this would be routing it like this:
controller/method/variable_name1/variable_value1/user_conf/username
...and the add the implementation to every single controller in my project. But this is not an elegant solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这就是您所追求的:
所有函数定义都将用户名作为最后一个参数:
或者,如果您只想在一个特定页面上,您可以执行以下操作:
或者,如果您希望它用于多个函数在控制器中你可以这样做:
Is this what you're after:
where all your function definitions have the username as the last parameter:
Or, if you only want in on one specific page you could do something like this:
Or, if you want it for a number of functions in a controller you could do this:
在解决这个问题一天之后,我最终将自定义路由器类添加到我的项目中。我正在使用 CodeIgniter 2.0,因此该文件的位置应该是
application/core/MY_Router.php
我的代码如下:
}
After messing with this problem for a day I ended up with adding custom router class to my project. I'm working in CodeIgniter 2.0, so the location of this file should be
application/core/MY_Router.php
My code is following:
}