在 Laravel 中插入一个变量到 Redirect::to 中


Inserting a variable into Redirect::to in Laravel

一旦

我完成了一些操作,我想使用 laravel 中的变量重定向::到自定义 url。这是我到目前为止的代码:

Route::get('remove_comment/{id}', function($id) {
    $comment = get_comment($id);
    $postId = $comment->Post_Id;
    remove_comment($id);
    return Redirect::to(url('page/{{{$postId}}}'));
});

所以我希望它重定向到带有/post/1 或类似 URL 的页面。它不起作用,我是如何实现它的,但我相信会有办法。可以按照我的要求做吗?

看起来你在路由内,因此除非指定,否则使用 Blade 将不起作用。此外,它已经在 PHP 中,所以这样做:

Route::get('remove_comment/{id}', function($id) {
    $comment = get_comment($id);
    $postId = $comment->Post_Id;
    remove_comment($id);
    return Redirect::to(url('page/'.$postId));
});

请注意这一行:return Redirect::to(url('page/'.$postId));

您还可以使用双引号而不是单引号将变量注入字符串中,并将变量括在大括号中。所以: return Redirect::to(url("page/{$postId}"));