Code Monkey home page Code Monkey logo

Comments (12)

kasparrov avatar kasparrov commented on May 29, 2024 1

{slug?} done the trick, thx a lot!

from laravel-localized-routes.

ivanvermeyen avatar ivanvermeyen commented on May 29, 2024

Hello,

The / route needs to get the active locale from the session, which is done by Localizer.
So I think it should work when you set use_localizer to true.

from laravel-localized-routes.

kasparrov avatar kasparrov commented on May 29, 2024

have the same issue with or without localizer
tried:

Route::localized(function () {
    Route::get('/', 'PageController@renderIndex');
    Route::get('{page}', 'PageController@render');
});

and

Route::localized(function () {    
     Route::get('{page}', 'PageController@render')->where('page', '.*');
});

from laravel-localized-routes.

NadjmanDev avatar NadjmanDev commented on May 29, 2024

Hi,
I think i found the solution.
In the config file ( localized-routes.php ), if your omit_url_prefix_for_locale is set to en so in supported-locales you have to add en in the last postition like this example below.

return [

    'supported-locales' => ['pt', 'fr', 'en'],

    'omit_url_prefix_for_locale' => 'en',

    'use_locale_middleware' => true,

    'use_localizer' => false,

];

I hope it helps you

from laravel-localized-routes.

ivanvermeyen avatar ivanvermeyen commented on May 29, 2024

Hi,

I was able to reproduce it in a test.
It seems the routes are being registered like this:

  #items: array:4 [
    0 => "/"
    1 => "{slug}"
    2 => "nl"
    3 => "nl/{slug}"
  ]

So in this case /nl can not be accessed. The {slug} intercepts it.
Looking into it now. 👍

from laravel-localized-routes.

kasparrov avatar kasparrov commented on May 29, 2024

yep, i have the same route registration sequence. Sorry to not attach it before.
Thx for a fast responce!

from laravel-localized-routes.

ivanvermeyen avatar ivanvermeyen commented on May 29, 2024

@NadjmanDev moving the omitted locale to the end seems like the best solution, thanks 👍
Will tag it when tests completed.

from laravel-localized-routes.

ivanvermeyen avatar ivanvermeyen commented on May 29, 2024

Fix has been tagged in 2.2.7 👍

from laravel-localized-routes.

kasparrov avatar kasparrov commented on May 29, 2024

moving the omitted locale works only when you register the same route twice(for root and for anything else)

Route::localized(function () {
    Route::get('/', 'PageController@renderIndex');
    Route::get('{page}', 'PageController@render');
});

and doesnt work when i tried

Route::localized(function () {    
     Route::get('{page}', 'PageController@render')->where('page', '.*');
});

checked with 2.2.7 tag

from laravel-localized-routes.

ivanvermeyen avatar ivanvermeyen commented on May 29, 2024

What is the behavior you see?

I don't immediately see why it would make any difference. (but then again it is confusing) :)

from laravel-localized-routes.

kasparrov avatar kasparrov commented on May 29, 2024

I created draft project with 3 route groups(api,admin, web)
at web section i have only

Route::localized(function () {
    Route::get('{page}', 'PageController@render')->where('page', '.*');
});

definition for render function:

public function render(Request $request, $page = null)

config:

'supported-locales' => ['be','en','ru'],
'omit_url_prefix_for_locale' => 'ru',
'use_locale_middleware' => true,
'use_localizer' => false,

roule list:

 POST     | admin/api/token |      |
 GET|HEAD | api/test        |      |
 GET|HEAD | be/{page}       | be.  |
 GET|HEAD | en/{page}       | en.  |
 GET|HEAD | {page}          | ru.  |

with this config
domain.com/ - get page variable value null
domain.com/en - get page variable value "en"
domain.com/be - get page variable value "be"
domain.com/ololo - get page variable value "ololo"
domain.com/en/ololo - get page variable value "ololo"
domain.com/be/ololo - get page variable value "ololo"

workaround - change routes to:

Route::localized(function () {
    Route::get('/', 'PageController@render');
    Route::get('{page}', 'PageController@render')->where('page', '.*');
});

from laravel-localized-routes.

ivanvermeyen avatar ivanvermeyen commented on May 29, 2024

This test passes for me.
Notice the {slug?} placeholder.
Does this solve your use case?

/** @test */
public function it_registers_a_dynamic_root_route_without_prefix_for_a_configured_main_locale()
{
    $this->setSupportedLocales(['en', 'nl']);
    $this->setOmitUrlPrefixForLocale('en');
    $this->setUseLocaleMiddleware(true);

    Route::localized(function () {
        Route::get('{slug?}', function ($slug = null) {
            return ($slug ?? 'null') . ' ' . App::getLocale();
         })->where('page', '.*');
    });

    $this->assertEquals(
        ['nl/{slug?}', '{slug?}'],
        $this->getRoutes()->pluck('uri')->toArray()
    );

    $response = $this->call('GET', '/');
    $response->assertOk();
    $this->assertEquals('null en', $response->original);

    $response = $this->call('GET', '/nl');
    $response->assertOk();
    $this->assertEquals('null nl', $response->original);

    $response = $this->call('GET', '/dynamic');
    $response->assertOk();
    $this->assertEquals('dynamic en', $response->original);

    $response = $this->call('GET', '/nl/dynamic');
    $response->assertOk();
    $this->assertEquals('dynamic nl', $response->original);
}

from laravel-localized-routes.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.