【问题标题】:codeigniter pass the parameter before the method in the url?codeigniter在url中的方法之前传递参数?
【发布时间】:2013-05-07 15:05:28
【问题描述】:

我试图将参数传递给 CodeIgniter 上的控制器,但我希望 url 是这样的:

mywebsite/profile/{user_id}/about

不是这样的:

mywebsite/profile/about/{user_id}

所以有可能吗?我们该怎么做?

【问题讨论】:

  • 可以肯定的是,profile是控制器名称,about是功能?
  • 我不确定,是否有办法这样做。如果有,它将在 routes.php 中。试试$route['about/(:any)'] = '$1/about' 或者$route['(:any)/about'] = 'about/$1' 之类的东西(对路由系统不太熟悉了)

标签: php codeigniter url


【解决方案1】:

这对我有用,我刚刚测试过:

在 routes.php 中创建一个新路由,如下所示:

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

这将重写 URL,例如“yoursite.com/profile/about/23”到“yoursite.com/profile/23/about”。我假设,“profile”是你的控制器,而“about”是函数。

希望,这会有所帮助!不过,没有使用来自数据库的 ID 对其进行测试。

【讨论】:

  • 如果你的身份证是严格的数字,你最好使用:num而不是:any
  • 不知道 :num,但是,那将是更合适的解决方案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-14
  • 1970-01-01
  • 1970-01-01
  • 2015-05-01
  • 2021-05-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多