simple-php-router icon indicating copy to clipboard operation
simple-php-router copied to clipboard

Multi lang hintting

Open testt23 opened this issue 3 years ago • 2 comments

Hi, thanks for this brilliant packet!

But.. I have a one small problem. What I want to make? If someone people is visit my site: www.site.com is the look for default language: English..but if is want to change to France...

    SimpleRouter::partialGroup( '{lang}/', function ($lang = 'en') {
        SimpleRouter::get('/', 'DefaultController@index')->setName('home');  //output: Route not found: "/"
        SimpleRouter::get('/contact', 'DefaultController@contact')->setName('contact');
        SimpleRouter::basic('/companies/{id?}', 'DefaultController@companies')->setName('companies');
    });

I make this. site.com/ and site.com/en => is default site.com/fr/ => France language

the small problem is that the way i do it is .. that i want the pages that are added to the language itself to also get the default language. If you looking the www.site.com/contact (this is the english version) , but to now is not working. To now is working www.ste.com/en/contact

www.site.com/ -> error no working | www.site.com/en -> working correctly www.site.com/contact -> error no working | www.site.com/en/contact -> working correctly www.site.com/companies/id/1 -> error no working | www.site.com/en/companies/id/1 -> working correctly

I experimented only with a group: but it still doesn't work the way I want. What can I do?


    Router::group([
        'prefix' => '{lang}',
    ], function ($lang = 'en'){
        SimpleRouter::get('/', 'DefaultController@index')->setName('home'); //output: Route not found: "/"
        SimpleRouter::get('/contact', 'DefaultController@contact')->setName('contact');
        SimpleRouter::basic('/companies/{id?}', 'DefaultController@companies')->setName('companies');
    });

And I check and with this: 'prefix' => '{lang?}',

But nothing.

testt23 avatar Nov 30 '21 01:11 testt23

Hello,

my code for that isn't perfect, but here is how I did it. First I registered three routes:

Router::get('/', '\rpcms\website\controllers\RedirectController@noLanguage');
Router::get('/{language}/', 'IndexController@renderPageView')->where(array('language' => 'de'));
Router::get('/{language}/', 'IndexController@renderPageView')->where(array('language' => 'en'));

OR

Router::get('/{language}/ueber-uns', 'AboutController@renderPageView')->where(array('language' => 'de'));
Router::get('/ueber-uns', '\rpcms\website\controllers\RedirectController@noLanguage');
Router::get('/{language}/about', 'AboutController@renderPageView')->where(array('language' => 'en'));
Router::get('/about', '\rpcms\website\controllers\RedirectController@noLanguage');

Providing a RegEx for language using ->where() I ensure that I still get the language as a parameter. The IndexController then provides the user with the content in the correct language.

In the RedirectController you then have to handle a request if no language is provided. I did this by finding the correct Controller by the route and then called him with the language as a parameter. This can be done by a redirect. The language can be a default language, the language provided by the request headers or the language of the previous page stored in for example a session.

I hope that I could help you.

~ Marius

DeveloperMarius avatar Feb 02 '22 00:02 DeveloperMarius

Hello, if you don't need this functionality anymore or my comment helped you, please close your issue. Thank you.

~ Marius

DeveloperMarius avatar Feb 19 '22 17:02 DeveloperMarius