M
M
Mokhirjon Naimov2016-05-03 21:24:26
Laravel
Mokhirjon Naimov, 2016-05-03 21:24:26

How to automatically add an optional parameter for every generated URL in Laravel 5?

I have a route like this:

Route::group([
  'prefix'    => '{lang?}',
  'as'        => 'frontend.',
  'namespace' => 'Frontend'
],
function ()
{
    Route::get('articles', [
    	'as'   => 'articles',
    	'uses' => '[email protected]'
    ]);
});

Now, when the route() helper is invoked, for example, route('frontend.articles') I need it to return such a url ` http://somesite.ru/articles` if there was no prefix parameter, and if there was (for example, prefix = ' en') then such a url ` http://somesite.ru/en/articles`.
How will this be achieved?
UPD: So I don't want to always manually specify the prefix parameter in the route('frontend.articles', ['prefix' => App::getLocale()]) helper.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mokhirjon Naimov, 2016-05-12
@zvermafia

I used the mcamara / laravel-localization package, it turns out that this is already implemented ...

R
Rustamka Vorontsov, 2016-05-03
@rmfordev

I would generate a url in the model like

public function route(array $params = [], $absolute_url = true) {
        $params = array_diff($params, ['', null]);

        return route('frontend.articles', array_replace($params, ['prefix' => App::getLocale()]), $absolute_url);
}

and call $article->route(other parameters)
or you can specify it in the base model, and all models will already have the ->route(..) parameter

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question