使用 Laravel 重定向到没有获取参数的路由

Redirect to route without get params with Laravel

我有一些带有 get 参数的遗留 urls,我想在没有这些 get 参数的情况下重定向到路由。

在我的 web.php 我有:

Route::get('/', ['as' => 'welcome', 'uses' => 'PageController@welcome']);

http://example.com/?page_id=5 should redirect (302) to http://example.com/.

这样的 URL

在控制器中我尝试了以下操作:

public function welcome(Request $request)
{
    if($request->has('page_id')) {

        redirect()->to('welcome', 302);
    }

    return view('welcome');
}

它到达了重定向,但 url 中仍然有 ?page_id=5。类似于:

redirect()->to('welcome', 302)->with('page_id', null);

同样没有区别。在 Laravel 5.3 中重定向带有参数的页面的最佳方法是什么?到一个没有参数的?

你应该在 redirect() 方法前面使用 return 才能使其工作:

public function welcome(Request $request)
{
    if($request->has('page_id')) {

        return redirect()->route('welcome');
    }

    return view('welcome');
}

希望对您有所帮助!