【发布时间】:2021-10-31 03:19:28
【问题描述】:
从 url 隐藏 id
https://wallpaperaccess.in/photo/162/download-wallpaper
我想要这样的网址
https://wallpaperaccess.in/photo/download-wallpaper
ImagesController.php
public function show($id, $slug = null ) {
$response = Images::findOrFail($id);
$uri = $this->request->path();
if( str_slug( $response->title ) == '' ) {
$slugUrl = '';
} else {
$slugUrl = '/'.str_slug( $response->title );
}
$url_image = 'photo/'.$response->id.$slugUrl;
//<<<-- * Redirect the user real page * -->>>
$uriImage = $this->request->path();
$uriCanonical = $url_image;
if( $uriImage != $uriCanonical ) {
return redirect($uriCanonical);
}
路线
// Photo Details
Route::get('photo/{id}/{slug?}','ImagesController@show');
注意:我在数据库中没有任何 slug 列,所以我们可以使用 title 作为 slug 吗?
【问题讨论】:
-
如果你想从 url 中隐藏 id,你应该将你的路由编辑为
Route::get('photo/{slug?}','ImagesController@show');,然后你还必须将你的方法编辑为public function show($slug = null )。不确定这是否是您要查找的内容,因为看起来您正在使用 id 来查找图像? -
这是可能的
Route::get('photo/{slug?}','ImagesController@show');将id值存储在会话中,并从会话Images::findOrFail(session()->get('id'));获取到控制器上 -
可以用uuid替换bigint id吗?
-
很简单,你只需在 url 中加密和 dycript 你的 id
-
我将我的路线编辑为
Route::get('photo/{slug?}','ImagesController@show');,并将方法编辑为public function show($slug = null ),但它仍然显示 id 瞬间