Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

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?)

like image 573
Fabian Avatar asked Feb 11 '16 15:02

Fabian


1 Answers

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

url()->current()

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

Route::current()->getName()

Now you can use this route name to create your own new 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.

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

It's pretty simple and easy to use.

like image 90
Jilson Thomas Avatar answered Nov 19 '22 23:11

Jilson Thomas