[英]How can I pass part of an url to my controller in Laravel
我正在制作一個注冊表單,在填寫表格后,用戶會得到一個 email,其中包含他們提交的所有信息,包括一個可以編輯注冊的鏈接。 示例 url: localhost/registrationapp/edit/{id}
我一直在嘗試將 url 的一部分傳遞給 controller,這是我的路線:
Route::get('/edit/{id}', [RegistrationappController::class, 'edit'])->with('id', $id);
我在我的 controller 中得到了這個 function:
public function edit($id)
{
return 123;
$registration= Registrationapp::find($id);
return view('edit')->with('registration, $registration);
}
只是添加了return 123
部分,看看我是否可以到達 controller,但它沒有到達 controller。 相反,當我將 go 轉換為 url (例如localhost/registrationapp/edit/5
)時,我會收到此錯誤:
Undefined variable $id
有什么辦法可以做我想做的事嗎? 任何幫助將不勝感激。
我認為您需要將路線設置為Route::get('/registrationapp/edit/{id}', [TestController::class, 'edit']);
.
請隨時查看我的 laravel 7 代碼段https://phpsandbox.io/n/73314638-5fvdc 。 它演示如下:
Route::get('/registrationapp/edit/{id}', [TestController::class, 'edit']);
注意,我借用了默認的welcome.blade.php
文件,並更新了web.php
中的默認/
路由。
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class TestController extends Controller
{
public function edit($id)
{
return view('welcome', ['id' => $id]);
}
}
編輯:您在接受答案的評論回復中是對的,我錯過了您分享的示例 URL 的前綴,對此表示歉意。 我在這里更新了我的答案。
只需刪除->with('id', $id);
注意:如果您使用的是 url: localhost/registrationapp/edit/{id}
,請確保在路由中包含registrationapp
:
Route::get('registrationapp/edit/{id}', [RegistrationappController::class, 'edit']);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.