如何在laravel中将变量从视图传递到路由 [英] how to pass variable from view to route in laravel

查看:34
本文介绍了如何在laravel中将变量从视图传递到路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这是我的 route.php 中的代码:

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

welcome.blade.php 上:

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

我想建立一个引用以上路线的链接.

I want to build a link referencing the route above.

推荐答案

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

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:

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>

请注意,第一个参数代表路由名称,第二个参数代表您的URL.您可以在此处了解更多信息.

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.

这篇关于如何在laravel中将变量从视图传递到路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆