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.4] Default named parameters in UrlGenerator #16736

Merged
merged 3 commits into from
Dec 11, 2016
Merged

[5.4] Default named parameters in UrlGenerator #16736

merged 3 commits into from
Dec 11, 2016

Conversation

pilot911
Copy link
Contributor

How to use default named parameters (localize etc).

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

or

app("url")->setDefaultNamedParameters(["locale", app()->getLocale(), "userId" => app("auth")->user()->getKey()]);

Just create middleware

<?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->setDefaultNamedParameters("locale", config('app.locale'));

        return $next($request);
    }
}

In routes/web.php file

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>

How to use default named parameters (localize etc).
```
app("url")->setDefaultNamedParameters("locale", app()->getLocale());
```
or
```
app("url")->setDefaultNamedParameters(["locale", app()->getLocale(), "userId" => app("auth")->user()->getKey()]);
```
Just create middleware

```
<?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->setDefaultNamedParameters("locale", config('app.locale'));

        return $next($request);
    }
}
```
In routes/web.php file
```
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>
```
@GrahamCampbell GrahamCampbell changed the title Default named parameters in UrlGenerator.php [5.4] Default named parameters in UrlGenerator Dec 10, 2016
@pilot911 pilot911 mentioned this pull request Dec 11, 2016
@taylorotwell taylorotwell merged commit f72ac7f into laravel:master Dec 11, 2016
greut referenced this pull request in HE-Arc/EventOrganizer Dec 12, 2016
greut referenced this pull request in HE-Arc/RebelLegion Dec 14, 2016
…sateurs, leur modele, leur vues, corrrection du bug des parametres dans le header et les routes
@pilot911 pilot911 deleted the patch-12 branch January 18, 2017 22:46
@pilot911 pilot911 restored the patch-12 branch January 18, 2017 22:47
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