[英]Laravel pass value from controller to route and then to controller
Working my way through my first Laravel project. 在我的第一个Laravel项目中工作。 Thanks for all the help.
感谢您的所有帮助。
I have a SearchController that says if the count of the query is more then 1 to go to a view provided. 我有一个SearchController,如果查询的数量大于1,则转到提供的视图。
return view('pages/multi-providers')->with('data',$data)->with('city',$city)->with('state',$state)->with('provider',$provider)->with('zipcode',$zipcode);
I am able to pass the variables that were created in that simple way. 我能够传递以这种简单方式创建的变量。 But then the route is www.domain.com/search/?q=01035
但随后的路线是www.domain.com/search/?q=01035
So I tried to do a return redirect to a named route 所以我尝试将返回重定向到命名路由
return redirect()->route('multi-providers',[$zipcode])->with('data',$data)- >with('city',$city)->with('state',$state)->with('provider',$provider)->with('zipcode',$zipcode);
Then my route in my routes.php is 然后我的routes.php中的路线是
Route::get('multi-providers/{zipcode}', ['uses' => 'PagesController@multiProviders','as' => 'multi-providers'])->with('data',$data)->with('city',$city)->with('state',$state)->with('provider',$provider)->with('zipcode',$zipcode);
And then in my PageController@multiProviders I have 然后在我的PageController @ multiProviders中
public function multiProviders($zipcode)
{
$data['zipcode'] = $zipcode;
return view('pages/multi-providers')->with('data',$data)->with('city',$city)->with('state',$state)->with('provider',$provider)->with('zipcode',$zipcode);
}
The route begins to work and it does show as 该路线开始起作用,并且确实显示为
www.domain.com/multi-provider/{zipcode}(acutally shows zip like 92804)
But then I get an error that the $city and $state variables are not defined. 但是然后我得到一个错误,即未定义$ city和$ state变量。 So I tried passing them like the zipcode in the public function but then it says its missing an argument.
因此,我尝试在公共函数中像邮政编码那样传递它们,但随后它说缺少参数。 Im guessing cause I dont have a extra parameter in the route like {city} or {state} in the url.
我猜是因为我在网址中没有像{city}或{state}这样的路由中有额外的参数。 Is there way to pass those variables without them being in the URL?
有没有办法传递这些变量而无需将它们放在URL中? Or a different way to store them.
或以其他方式存储它们。 I may be doing it a harder way then it should be.
我可能会比应该做的难。 Thanks again for your time and help.
再次感谢您的时间和帮助。
Have you tried doing this with a "compact"? 您是否尝试过使用“紧凑”功能?
Like this in your public controller: 在公共控制器中是这样的:
return view('pages.multi-providers', compact('zipcode', 'data', etc. etc.));
This makes it a little bit more readable and should pass those variables correctly. 这使它更具可读性,应该正确传递这些变量。
Route::get('multi-providers/{zipcode}/{city}/{state}/{provider}/{data}', ['uses' => 'PagesController@multiProviders','as' => 'multi-providers']);
and then you get in these variables from controller like this 然后像这样从控制器获取这些变量
public function multiProviders($zipcode, $city, $state, $provider, $data)
{
return view('pages/multi-providers', compact('zipcode', 'city', 'state', 'provider', 'data'))}
And to go to this route with these parameters you should do this 要使用这些参数进入此路线,您应该执行此操作
return redirect()->route('multi-providers',compact('zipcode', 'city', 'state', 'provider', 'data'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.