Skip to content

Conversation

@mackiecarr
Copy link

Allow the UrlGenerator route(...) function to get its parameters from the current request as suggested in issue #7366. With this you can either explicitly set the parameters or get them from the request.

Example:

Route::group(
    [
        'domain' => '{subdomain}.domain.com',
    ],
    function ($router) {
        Route::get(
            '/',
            [
                'uses' => function ($subdomain) use ($router) {
                    $route = $router->getCurrentRoute();
                    $action = $route->getAction();
                    $action['domain'] = $subdomain . '.domain.com';
                    $route->setAction($action);

                    return route('subhome');
                },
                'as' => 'subhome'
            ]
        );
    }
);

You can use route('subhome') with no other options and have it generate a correct URL.

Just a suggestion :)

…d routes

A proposed solution for issue laravel#7366. For the route method if there are no parameters, get the parameters from the current request to correctly parse a URL.

~~~
Route::group(
    [
        'domain' => '{subdomain}.domain.com',
    ],
    function ($router) {
        Route::get(
            '/',
            [
                'uses' => function ($subdomain) {
                    return route('subhome');
                },
                'as' => 'subhome'
            ]
        );
    }
);
~~~

Currently this would generate:
~~~
http://%7Bsubdomain%7D.domain.com
~~~

By getting the parameters from the current request it now generates this:
~~~
http://blah.domain.com
~~~

Not sure if this is the best "solution" just thought I would share :)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for the else block. Do this:

if (count($parameters) > 1) {
    return $this->toRoute($route, $parameters, $absolute);
}

return $this->toRoute($route, $route->bind($this->getRequest())->parameters(), $absolute);

@mackiecarr
Copy link
Author

Made those changes, thanks!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One too many tabs.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, you need the new lines as I put them. Don't remove them.

@GrahamCampbell
Copy link
Collaborator

Better to replace this whole thing with this code:

    public function route($name, $parameters = array(), $absolute = true)
    {
        if (is_null($route = $this->routes->getByName($name)))
        {
            throw new InvalidArgumentException("Route [{$name}] not defined.");
        }

        if (count($parameters) > 0) {
            return $this->toRoute($route, $parameters, $absolute);
        }

        return $this->toRoute($route, $route->bind($this->getRequest())->parameters(), $absolute);
    }

@taylorotwell
Copy link
Member

Still don't really like this.

@mackiecarr mackiecarr deleted the patch-2 branch February 12, 2015 18:42
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.

3 participants