Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.3] Add default parameters to UrlGenerator #16650

Closed
wants to merge 4 commits into from
Closed

[5.3] Add default parameters to UrlGenerator #16650

wants to merge 4 commits into from

Conversation

pilot911
Copy link
Contributor

@pilot911 pilot911 commented Dec 4, 2016

Adding global parameters like 'locale' for creating URL by named routers without passing smth like ['locale' => config('app.locale')] in every url generator on page.

app("url")->setDefaultParameters("locale", app()->getLocale());

and create URLs via

app('router')->pattern('locale', '(' . implode('|', config('app.locales')). ')');
app('router')->group(['domain' => '{locale}.telenok.com'], function ()
{
    app("router")->post("some-url", array("as" => "some-name", "uses" => "SomeClass@somemethod"));
});

Then instead of

<a href="{{  route("some-name") }}">.....</a>

you'll get

<a href="http://en.telenok.com/some-url">.....</a>

Now we can add in out providers global parameters like 'locale' to fill named routers without passing ['locale' => <current-locale>] in every url generation on pages.
@pilot911 pilot911 mentioned this pull request Dec 4, 2016
@pilot911 pilot911 changed the title Update UrlGenerator.php Add default parameters to UrlGenerator Dec 4, 2016
@pilot911 pilot911 changed the title Add default parameters to UrlGenerator Add default parameters to UrlGenerator. Localization etc Dec 5, 2016
@pilot911
Copy link
Contributor Author

pilot911 commented Dec 6, 2016

Better set global url parameters in middleware like

<?php

namespace App\Http\Middleware;

use Illuminate\Foundation\Application;

class UrlGeneratorParameters {

    protected $app;

    public function __construct(Application $app)
    {
        $this->app = $app;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, \Closure $next)
    {
        $this->app->url->setDefaultParameters("locale", config('app.locale'));

        return $next($request);
    }
}

@GrahamCampbell GrahamCampbell changed the title Add default parameters to UrlGenerator. Localization etc [5.3] Add default parameters to UrlGenerator Dec 7, 2016
@taylorotwell
Copy link
Member

This implementation will not work because if you have some routes that do not need the default parameter you will end up appending it to the query string:

Route::as('something')->get('/foobar/{baz}', function () {

});

Route::get('/', function () {
    dd(route('something', ['baz' => 1, 'default' => 'something']));

    return view('welcome');
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants