skipperbent / simple-php-router

Simple, fast and yet powerful PHP router that is easy to get integrated and in any project. Heavily inspired by the way Laravel handles routing, with both simplicity and expand-ability in mind.
651 stars 123 forks source link

Multi lang hintting #600

Open testt23 opened 2 years ago

testt23 commented 2 years ago

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.

DeveloperMarius commented 2 years ago

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 commented 2 years ago

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

~ Marius