php Laravel:如何获取当前 url 作为带参数的视图助手

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35343119/
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-08-26 00:26:12  来源:igfitidea点击:

Laravel: How to get the current url as a view helper with parameters

phplaravelview-helpers

提问by Fabian

I want to build a link in my view that refers to the same page like that one where its placed on. And I want to be able to give a parameter with.

我想在我的视图中建立一个链接,该链接指向与放置它的页面相同的页面。我希望能够给出一个参数。

For example I want to change languages. I have a route like

例如,我想更改语言。我有一条路线

domain.com/{lang}/xyz

And in my view I want to do something like

在我看来,我想做类似的事情

<a href="{{ URL::action(this, ['lang' => 'en']) }}">EN</a>

So I can easily reload the page but just change the "lang" parameter.

所以我可以轻松地重新加载页面,但只需更改“lang”参数。

Hopefully its understandable. Please try to help me.

希望它是可以理解的。请试着帮助我。

(Another side question: Are there no ressources eg a list of all view helpers in Laravel? where do i know which viewhelpers are available?)

(另一个问题:是否没有资源,例如 Laravel 中所有视图助手的列表?我在哪里知道哪些视图助手可用?)

回答by Jilson Thomas

Use laravel's helper method to use in a view:

使用 laravel 的 helper 方法在视图中使用:

url()->current()

This will get the current URL. If you need to get the current route name,

这将获得当前的URL. 如果您需要获取当前路线名称,

Route::current()->getName()

Now you can use this route name to create your own new URL.

现在您可以使用此路由名称来创建您自己的新 URL。

eg:

例如:

<a href="{{ URL::action(Route::currentRouteName(), ['lang' => 'en']) }}">EN</a>

Your route definition may be something like:

您的路线定义可能类似于:

Route::get('/{lang}/about/', ['as'=>'about_us', 'uses'=>'PagesController@about'])

This will provide you the current URL.

这将为您提供当前的 URL。

But in your case, it's better to use the this package for multi language: https://github.com/mcamara/laravel-localization

但在您的情况下,最好将此包用于多语言:https: //github.com/mcamara/laravel-localization

It's pretty simple and easy to use.

它非常简单且易于使用。