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);
}
}