Laravel 重定向到其他路由

Laravel redirect to other route

我有一个函数 getLocation,该路线被命名为 "search.location",例如 URL www.url.com/service/ny/slug。这显示了一个位置。

我现在想检查该位置是否可用。如果具有给定参数的位置不可用,我想调用具有参数服务和城市的路由 "search.servicecity"。 URL 就是 www.url.com/service/ny/。如何实现带参数调用路由?

public function getLocation($servicename, $city, $slug){
  $location = Location::where([
      ['slug','=',$slug],
      ['city','=',$city]
    ])->first();

  if(count($location)>0){
    return view('search.location')->with('servicename',$servicename)
                ->with('city',$city)->with('information','getLocation')
                ->with('slug',$slug)->with('location',$location);
  }else{
    //call search.servicecity with parameters $servicename and $city
  }
}

您可以为此使用 redirect 助手。

return redirect()->route('search.servicecity', [
    'service' => $servicename,
    'city'    => $city,
]);

查看 docs 了解其他示例。