Skip to content

Instantly share code, notes, and snippets.

@KazunagaIida
Last active March 25, 2017 17:10
Show Gist options
  • Save KazunagaIida/be95dcc42d7f194881dbab5a20ecd29d to your computer and use it in GitHub Desktop.
Save KazunagaIida/be95dcc42d7f194881dbab5a20ecd29d to your computer and use it in GitHub Desktop.
【Laravel5.3】url(), redirect()使用時にURLが変わる場合の対処方法 ref: http://qiita.com/kiida510/items/7b1b128eadca1eb8fde6
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$url = $this->app['url'];
$this->app->singleton('url', function () use ($url) {
return new CustomUrlGenerator($url);
});
}
<?php
namespace App\Providers;
use Illuminate\Routing\UrlGenerator;
/**
* Class CustomUrlGenerator
* @package App\Providers
*/
class CustomUrlGenerator extends UrlGenerator
{
/**
* Create a new manager instance.
*
* @param Illuminate\Routing\UrlGenerator $url
*/
public function __construct(UrlGenerator $url)
{
parent::__construct($url->routes, $url->request);
}
/**
* Generate an absolute URL to the given path.
*
* @param string $path
* @param mixed $extra
* @param bool $secure
* @return string
*/
public function to($path, $extra = [], $secure = null)
{
// First we will check if the URL is already a valid URL. If it is we will not
// try to generate a new one but will simply return the URL as is, which is
// convenient since developers do not always have to check if it's valid.
if ($this->isValidUrl($path)) {
return $path;
}
$scheme = $this->getScheme($secure);
$extra = $this->formatParameters($extra);
$tail = implode('/', array_map(
'rawurlencode', (array) $extra)
);
// Once we have the scheme we will compile the "tail" by collapsing the values
// into a single string delimited by slashes. This just makes it convenient
// for passing the array of parameters to this URL as a list of segments.
// Original
//$root = $this->getRootUrl($scheme);
$root = 'http://example.co.jp';
if (($queryPosition = strpos($path, '?')) !== false) {
$query = mb_substr($path, $queryPosition);
$path = mb_substr($path, 0, $queryPosition);
} else {
$query = '';
}
return $this->trimUrl($root, $path, $tail).$query;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment