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

22,139

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.

Share:
22,139
Fabian
Author by

Fabian

Updated on March 08, 2020

Comments

  • Fabian
    Fabian about 4 years

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