我正在開發推薦系統,但我被卡住了,如何做到這一點。當為產品生成一個推薦人鏈接時,我想在任何人點擊該鏈接時將用戶重定向到該產品。http://127.0.0.1:8000/affiliate/referrer_id/product_id這是一個推薦者鏈接的例子。
我生成了這個鏈接
http:/127.0.0.1:8000/affiliate/1/9
正如我們所看到的,在網址的末尾,產品ID是存在的。這個id鏈接的產品詳情頁是這樣的
http:/127.0.0.1:8000/product/9。
我的問題是,當有人點擊推薦者鏈接時,如何將用戶重定向到這個產品的詳細鏈接?
我嘗試了這個方法,但我不確定這是否正確。 在web.php中,我宣告了一個像這樣的路由
。 // Affiliate
Route::get('http://127.0.0.1:8000/affiliate/{referrer_id}/{product_id}', 'UsersController@referrerRedirect')。)
在UsersController referrerRedirect函式中
public function referrerRedirect($product_id, $referrer_id)
{
return view()。
}
謝謝!
uj5u.com熱心網友回復:
假設你有一個產品路由和控制器。有很多方法來設定路由(例如資源控制器等),這只是一個例子
。Route::get('products/{product_id}', 'ProductController@get')-> name('product') 。
public function get($product_id)
{
$product = Product::findOrFail($product_id)。
return view('your_product_view', ['product' => $product] )。
}
這個想法是,你回傳一個重定向到產品路由->name()并傳遞引數。
public function referrerRedirect($product_id, $referrer_id)
{
//do referrel things with the $referrer_id.
return redirect()->route('product', [$product_id]) 。
/or 它可能是
return redirect()->route('product', ['product' => $product_id] )。)
}
uj5u.com熱心網友回復:
web.php
// Affiliate
Route::get('affiliate/{product_id}/{referrer_id}', 'UsersController@referrerRedirect') 。
UsersController referrerRedirect函式
public function referrerRedirect($product_id, $referrer_id)
{
// Right a query to fetch data from database product table match with $product_id .
$product = Product::where('id', $product_id)->get() 。
return view('products.details')->with( compact('product'))。
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/320523.html
標籤:
下一篇:如果內容不同,則重定向網站
