简体   繁体   English

CakePHP,是否可以在网址前面使用记录ID

[英]CakePHP, Is it possible to use a record id in the front of the url

I need to set a record id in the url for all my pages. 我需要在所有页面的URL中设置记录ID。 For example, if the user does not supply the id the site should always use 1000 as the default eg. 例如,如果用户不提供ID,则网站应始终使用1000作为默认值,例如 http:// www.demosite.com/1000. http:// www.demosite.com/1000。

If the user navigates to http:// www.demosite.com/1001 I set record 1001 details in a session and display it in an element. 如果用户导航到http:// www.demosite.com/1001,则在会话中设置记录1001详细信息并将其显示在元素中。 From here on forward I need all the urls to look like this. 从这里开始,我需要所有网址看起来像这样。

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

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

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

I have tried the route below, which works but I need to redirect and then I lose the 1001 record id in the url. 我已经尝试了下面的路由,该路由可以工作,但是我需要重定向,然后我在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]+'));

Can anyone help me with this please? 有人可以帮我吗?

You taken it bit complicated, you just need to pass it in the method in controller, define 1000 default there and then other parameters. 您把它弄复杂了,只需要在控制器的方法中传递它,在那定义1000个默认值,然后再定义其他参数即可。

function index($id=1000){

}

when you will pass new id into method that will over right new one 当您将新的id传递给将覆盖新的id的方法时

And along with these you can also use htaccess to work with other url locations like 除此之外,您还可以使用htaccess来处理其他网址位置,例如

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

You can take help from http://www.yourhtmlsource.com/sitemanagement/urlrewriting.html 您可以从http://www.yourhtmlsource.com/sitemanagement/urlrewriting.html寻求帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM