简体   繁体   English

Laravel路由无法使用参数

[英]Laravel routes are not working with parameters

I have declared two laravel routes in web.php, but Facing 404 error. 我在web.php中声明了两个laravel路由,但是面临404错误。

I want to access route parameter values in controller and based on this selection I need to perform some processing based on parameters. 我想访问控制器中的路由参数值,并且基于此选择,我需要基于参数执行一些处理。

My web.php code 我的web.php代码

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');

});

My Controller Code 我的控制器代码

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'));
        }

here is how I am calling the route, 这就是我所说的路线

return '<a href="' . route('admin.transaction_verified', ['id'=> $row->id, 'tran-id'=> $row->tran_id]) . '" class="btn btn-info btn-rectangle"
                      data-toggle="tooltip" data-original-title="Edit">Send Email</a>

I want to access the route parameters and based on parameters there are some tasks need to be done. 我想访问路线参数,并且基于参数需要完成一些任务。 But I am facing the 404 error. 但是我正面临404错误。 I am unable to figure out what is wrong? 我无法弄清楚哪里出了问题?

There are a few problems I can see: 我可以看到一些问题:

  • According to the Laravel docs , route parameters: 根据Laravel docs ,路由参数:

    ... should consist of alphabetic characters, and may not contain a - character. ...应该由字母字符组成,并且不能包含-字符。 Instead of using the - character, use an underscore (_). 代替使用-字符,而使用下划线(_)。

    So parameter names like {user-id} and {tran-id} will not work. 因此,诸如{user-id}{tran-id}类的参数名称将不起作用。 Change them to (for example) {user_id} and {tran_id} . 将它们更改为(例如) {user_id}{tran_id}

  • Your route definition includes those 2 parameters in the URL: 您的路线定义在URL中包含这两个参数:

     .../{user-id}/{tran-id}... 

    When you generate the route using route() , you must give it 2 parameters with those same names. 使用route()生成路由时,必须给它提供两个具有相同名称的参数。 But your code gives it an id : 但是你的代码给了它一个id

     ...['id'=> $row->id, 'tran-id'=> $row->tran_id]... 

    You need to change that id to user-id , or considering the above problem, user_id . 您需要将该id更改为user-id ,或者考虑上述问题user_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM