【问题标题】:CakePHP, Is it possible to use a record id in the front of the urlCakePHP,是否可以在 url 前面使用记录 id
【发布时间】:2015-05-02 14:01:18
【问题描述】:

我需要在我所有页面的 url 中设置一个记录 ID。例如,如果用户不提供 id,则站点应始终使用 1000 作为默认值,例如。 http://www.demosite.com/1000。

如果用户导航到 http://www.demosite.com/1001,我会在会话中设置记录 1001 详细信息并将其显示在元素中。从这里开始,我需要所有的 url 看起来像这样。

http://www.demosite.com/1001/about-us

http://www.demosite.com/1001/wizard/step1

http://www.demosite.com/1001/prices/view/1

我已经尝试了下面的路线,该路线可行,但我需要重定向,然后我丢失了 url 中的 1001 记录 ID。

Router::connect('/:id', array('controller' => 'distributors', 'action' => 'session'), array('id' => '[0-9]+'));
Router::connect('/:id/**', array('controller' => 'distributors', 'action' => 'session'), array('id' => '[0-9]+'));

谁能帮我解决这个问题?

【问题讨论】:

  • 你想实现类似Localization功能的caekphp??
  • 是的,但使用随机记录 ID。

标签: php .htaccess cakephp routes url-redirection


【解决方案1】:

你觉得有点复杂,你只需要在控制器的方法中传递它,在那里定义1000个默认值,然后再定义其他参数。

function index($id=1000){

}

当您将新的 id 传递给将覆盖正确的新方法的方法时

除此之外,您还可以使用 htaccess 处理其他 url 位置 喜欢

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule ^([0-9]{6})$ scriptname.php?no=$1 [L]
</IfModule>

您可以向http://www.yourhtmlsource.com/sitemanagement/urlrewriting.html寻求帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-13
    • 1970-01-01
    • 2018-01-21
    • 1970-01-01
    相关资源
    最近更新 更多