我在web.php中声明了两条laravel路由,但是面临404错误。
我想访问控制器中的路由参数值,并且基于此选择,我需要基于参数执行一些处理。
我的web.php代码
public function validationrequest(Request $request)
{
$password = $request->header('Password');
$username = $request->header('Username');
$msisdn=$request->msisdn;
$useresponse=$request->userresponse;
$useroption=$request->useroption;
$hash=$request->hash;
$hash_key="CIA152515276585";
$data=$msisdn.$useresponse.$useroption.$hash_key;
$hashed = hash('sha512',$data);
if($username=="ngfs" && $password=="jfgd")
{
if($hashed==$hash)
{
//implemetation
$ref_id=self::gen_uuid();
$cus_name="greatB";
$total_amount=456;
$statuscode="00";
$statusMsg = "Successful";
$message="Testing";
$data = $total_amount.$statuscode.$statusMsg.$message.$hash_key;
$hashed = hash('sha512',$data);
$games = BBVN::where('game_code',$useresponse)->get();
User_Response::create([
'game_code' => $games->game_code,
'answer' => $useroption,
'msisdn' => $msisdn
]);
return response()->json([
'amount'=>$total_amount,
'statusCode'=>$statuscode,
'statusMessage'=>$statusMsg,
'message'=>$message,
'hash'=>$hashed
]);
}else
{
$notification1 = new NotificationLog();
$notification1->amount = $total_amount;
$notification1->msisdn = $msisdn;;
$notification1->error_message = "Invalid haskey";
$notification1->error_code = "99";
return response(['errorMsg' =>"99", 'description' =>"Invalid haskey"]);
}
}
else
{
$notification2 = new NotificationLog();
$notification2->amount = $total_amount;
$notification2->msisdn = $msisdn;;
$notification2->error_message = "Invalid haskey";
$notification2->error_code = "99";
return response(['errorMsg' =>"99", 'description' =>"Invalid username and password"]);
}
}
我的控制器代码
Route::group(
['namespace' => 'Admin', 'prefix' => 'admin', 'as' => 'admin.', 'middleware' => ['role:admin']],
function () {
Route::get('transaction-verified/{user-id}/{tran-id}',['uses' => 'ForgotPassTranVerifyController@transaction_verified'])->name('transaction_verified');
});
这是我叫路线的方式,
public function transaction_verified($id,$tran_id)
{
$user = User::find($id);
if($user)
{
$user->status = 'active';
$user->save();
$response = $this->broker()->sendResetLink(
$user->email
);
echo'<pre>'; print_r($response); exit;
$transaction = ForgotPassFineTransaction::find($tran_id);
if($transaction)
{
$transaction->status = 'valid';
$transaction->save();
}
$this->message = true;
return redirect(route('admin.transaction-verify'));
}
我想访问路线参数,并且根据参数需要完成一些任务。但是我正面临404错误。我无法弄清楚出了什么问题?
答案 0 :(得分:2)
我可以看到一些问题:
根据the Laravel docs,路由参数:
...应该由字母字符组成,并且不能包含-字符。代替使用-字符,而使用下划线(_)。
因此,像{user-id}
和{tran-id}
这样的参数名称将不起作用。将它们更改为(例如){user_id}
和{tran_id}
。
您的路线定义在URL中包含了这两个参数:
.../{user-id}/{tran-id}...
使用route()
生成路线时,必须给它提供两个具有相同名称的参数。但是您的代码给了它一个id
:
...['id'=> $row->id, 'tran-id'=> $row->tran_id]...
您需要将id
更改为user-id
,或考虑上述问题user_id
。