6

I use localization functionality in my web application. But I want to add App::getLocale() to url(). So for example, in my view when I add <a href="{{url('/admin')}}">link</a> I want to display the URL in HTML as http://localhost/mysite/en/admin. How can I do it?

Can I customize built-in URL helper function?

jedrzej.kurylo
  • 39,591
  • 9
  • 98
  • 107
Someone
  • 736
  • 2
  • 12
  • 29

4 Answers4

2

In Laravel 5.4, use the following

{{ url('Your_Prefix', 'login') }}

url just formats the whole path to the requested address, you can add whatever you want in it.

Petar-Krešimir
  • 444
  • 8
  • 22
1

url() method doesn't allow that, as it generates URL for the exact value that you provide as first argument. However it's possible to achieve what you need if you switched to route() method and defined a prefix for your routes.

// define a route group with a prefix in routes.php
Route::group(['prefix' => App::getLocale()], function() {
  Route::get('admin', ['as' => 'admin', 'uses' => 'AdminController@action']);
});

// generate prefixed URL
echo route('admin');

If your locale is en, then the above line should give you a URL like /en/admin

jedrzej.kurylo
  • 39,591
  • 9
  • 98
  • 107
0

I know this is an old question, but I ran into the same problem today. Based on the other answers, I came up with the following solution: I created a helper class which uses the Laravel url() helper function, but pre-processes the $path variable prior to calling the helper function. I created my own implementation for both url() and secure_url() helper functions.

This was a post action after I implemented localization in my application, for which I consulted this question.

Take a look at my solution. I hope it is useful for someone someday:

<?php

namespace App\Http\Helpers;

class Helpers {

    /**
     * Generate a url for the application.
     *
     * @param  string  $path
     * @param  mixed   $parameters
     * @param  bool    $secure
     * @return \Illuminate\Contracts\Routing\UrlGenerator|string
     */
    public static function url($path = null, $parameters = [], $secure = null) {
        $path = (string) $path;
        if (strlen($path) > 0 && $path[0] !== '/') {
            $path = '/' . $path;
        }
        return url(app()->getLocale() . $path, $parameters, $secure);
    }

    /**
     * Generate a HTTPS url for the application.
     *
     * @param  string  $path
     * @param  mixed   $parameters
     * @return string
     */
    public static function secure_url($path, $parameters = []) {
        return static::url($path, $parameters, true);
    }

}
-5

enter image description hereI found solution. Tested it in laravel 5.3. It worked! Could be useful for others. So it is worth share:

Enter vendor\laravel\framework\src\Illuminate\Foundation\helpers.php and modify code like that:

Just add your prefix before $path variable

Someone
  • 736
  • 2
  • 12
  • 29
  • 4
    Never edit a class in vendor folder. This folder is entirely managed by composer. Any modification in it will be erased when you will run `composer update`. – Alexandre Butynski Jan 13 '17 at 17:20
  • Alexandre is right, **don't do that**. Never edit anything inside of the vendor folder. As you can see from the screenshot, the function `url()` is defined only if it's not defined already. You could define your custom `url()` function inside of an helper file located in your project folder and then put it in the `autoload` section of your `composer.lock`. – Reddalo Jun 08 '17 at 13:16
  • Never ever do that. It will break the synchronization with production and your development code. – Amit Kumar Dec 22 '18 at 07:46