如何将变量从视图传递到 Laravel 中的路由

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29381836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:16:06  来源:igfitidea点击:

how to pass variable from view to route in laravel

laravellaravel-5laravel-routing

提问by user4557928

How to pass a variable from view to route in Laravel?

如何在 Laravel 中将变量从视图传递到路由?

Here's the code at my route.php:

这是我的代码route.php

Route::get('/{id}/{id1}', 'WelcomeController@index');

And at welcome.blade.php:

并在welcome.blade.php

< a href="{{URL::route('/{4}/{5}')}}">test</a>

I want to build a link referencing the route above.

我想建立一个引用上述路线的链接。

回答by Ravan Scafi

A good approach to do it would be to name your route and then reference it by name, passing the parameters needed. Here is how: At routes.php:

一个好的方法是命名您的路线,然后按名称引用它,传递所需的参数。方法如下: 在routes.php

Route::get('/{id}/{id1}', ['as' => 'welcome_index', 'uses' => 'WelcomeController@index']);

And at your view, you can do this:

在您看来,您可以这样做:

<a href="{{ route('welcome_index', [4, 5]) }}">test</a>

Notice that the first parameter represent the route name and the second, the parameters for your URL. You can read more here.

请注意,第一个参数表示路由名称,第二个参数表示 URL 的参数。您可以在此处阅读更多内容。

The advantages of naming a route is that you can change your route path later and your URL will still work with the code above.

命名路由的好处是你可以稍后更改路由路径,并且你的 URL 仍然可以与上面的代码一起使用。