How to get id from seo friendly url using php?
How do i get the id of the user from seo friendly url开发者_如何学C?
Here's the url (1 is the id)
http://www.website.com/users/edit/1
And, if the url is http://www.website.com/users/edit/1/stack-overflow/ in this case how do i find the id?
I am trying to get it as <?php user = User::find($_GET['id']); ?>
but id doesn't work.
<?php
$url = explode("/",($_SERVER["REQUEST_URI"]));
print_r($url);
?>
In your case $url[2] should have the ID that you are after...
The best way to accomplish this is using .htaccess
and mod_rewrite
to assign the $_GET
variables:
.htaccess:
RewriteBase /
RewriteRule ^users/edit/(.*)/(.*)$ users/edit/?user_id=$1&somethingelse=$2 [L,NC]
Then in your php script:
$user_id = $_GET['user_id'];
$somethingelse = $_GET['somethingelse'];
Uhm... I think you should tell us more about the class or framework you are using... But whatever you are doing can't work at all. Because $_GET would only contain something if you would actually pass parameters in the ?id=123 way in the url... So this find will search for nothing because $_GET is empty.
Parse the super global $_SERVER['REQUEST_URI']
, which will contain something like users/edit/1/stack-overflow/
, with regex or substr
and strpos
精彩评论