简体   繁体   中英

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. For example, if the user does not supply the id the site should always use 1000 as the default eg. 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. 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/wizard/step1

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.

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.

function index($id=1000){

}

when you will pass new id into method that will over right new one

And along with these you can also use htaccess to work with other url locations like

<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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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