Laravel - 如何将参数从控制器传递到路由并在另一个控制器中使用它?

Laravel - How to pass parameter from controller to route and use it in another controller?

我已经配置了如下资源路由

Route::resource('users', UserController::class);

当用户发布数据时,它将调用控制器中的存储方法,它将在其中添加数据并为 success/failure 设置一条消息。

public function store(Request $request)
{
    // return $request;
    $request->validate(
        [
            "firstName" => 'required',
            "lastName" => 'required',
            "phoneNo" => 'required',
            "email" => 'email:rfc,dns'
        ]
    );

    $date = date(now());
    $data = [
        'firstName' => $request->firstName,
        'lastName' => $request->lastName,
        'phoneNo' => $request->phoneNo,
        'email' => $request->email,
        'designation' => $request->designation,
        'status' => $request->status,
        'createdAt' => $date,
        'updatedAt' => $date,
    ];
    $user = Firebase::insertData($data, static::$collection);
    if ($user->id() != null) {
        $message = "User Created Successfully";
    } else {
        $message = "Something went wrong. Please contact System Admin with error code USR001";
    }
    return redirect()->route('users.index', ['message' => $message]);
}

这将重定向到同一控制器的索引方法。如何在索引方法中使用 $message 参数并将其发送到视图?我的索引方法如下

public function index()
{
    $userCollection = app('firebase.firestore')->database()->collection('users');
    $userData = $userCollection->documents();
    $response = [];
    $app = app();
    foreach ($userData as $data) {
        $user = $app->make('stdClass');
        $user->firstName = $data["firstName"];
        $user->lastName = $data["lastName"];
        $user->phoneNo = $data["phoneNo"];
        $user->email = $data["email"];
        $user->designation = $data["designation"];
        $user->status = $data["status"];
        $user->createdAt = $data["createdAt"];
        $user->updatedAt = $data["updatedAt"];
        array_push($response, $user);
    }
    return view('pages.user.list-user', ['response' => $response]);
}

您可以使用带有重定向方法的 with() 方法直接将消息作为 flash 消息传递。

将您的重定向代码编辑为:

return redirect()->route('users.index')->with('message', $message]);

并在您的 pages.user.list-user blade 文件中添加以下代码:

@if (session('message'))
    <div class="alert alert-success">
        {{ session('message') }}
    </div>
@endif

访问 https://laravel.com/docs/8.x/redirects#redirecting-with-flashed-session-data 了解有关使用即显消息进行重定向的更多信息。

替换您的重定向代码:

return redirect()->route('users.index', ['message' => $message]);

return view('pages.user.list-user', ['message' => $message]);

(1)首先在index函数的参数中传递消息:

public function index($message)
{
...
}

(2) 这个没关系,你写对了:

return redirect()->route('users.index', ['message' => $message]);

(3) 现在只需访问视图中的消息 (blade) 并打印它:

{{ $message }}

您还可以将消息存储在 $response 数组中,然后只需将 $response 传递给所需的视图即可:

$response['message'] = $message;

您可以像在控制器中使用参数一样使用索引方法。

public function index(Request $request)
{
   // Your code
   $message = $request['message'];
}

如果您想访问查看中的消息,请使用

return redirect()->route('users.index')->with('message', $message]);

并使用 session('message') 从视图访问,就像 OMi Shah 的回答