Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Router multilang #6

Open
testt23 opened this issue Nov 23, 2021 · 1 comment
Open

Router multilang #6

testt23 opened this issue Nov 23, 2021 · 1 comment

Comments

@testt23
Copy link

testt23 commented Nov 23, 2021

If I add a class for multi language I encounter a problem with the Router class. How can we together adjust a Router class to add an option and check if there is any added language and if that language opens in any of the following examples:
https://**en.site.com
https://site.com**/en/

or....
https://site.com -> en
https:/site.de -> de
https://site.it -> it

@strifejeyz
Copy link
Owner

strifejeyz commented Nov 24, 2021

The Router class is only to render a page. This doesn't work exactly as other frameworks but I recommend that you instead create a separate layout or a view for other languages.

For example, if someone visits https://site.com/de you could capture that /de and render a view e.g. app/views/languages/de.php (Option #1) or if the domain is like https:/site.de you can instead use the Option #2

OPTION #1: Your first scenario: using the https://site.com/en

Add a route inside app/routes.php (You can use :any :int or :str):
get('/:any', 'HomeController@index');

Modify app/controllers/HomeController.php:

<?php
class HomeController
{
    // based on the route we added above, access it as a parameter:
    public function index($language = null) {
        if (!empty($language)) {
              return View::render("languages/$language"); // auto load view based on url.
        }
        else {
              return View::render('index'); // default view
        }
    }
}

OPTION #2: For your second scenario: https://site.de

Find a way to get the Top-level domain (TLD), in this case: "de"
this page will help: https://stackoverflow.com/questions/39118999/get-tld-from-url-via-php/39515132

You don't need to touch the app/routes.php, now in your app/controllers/HomeController:

<?php
class HomeController
{
    public function index() {
         // do your necessary logic here to capture the TLD
         $tld = 'de';
         return View::render("languages/$tld"); 
    }
}

I hope this will help you. You can help me improve this project let me know if you're interested.

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

No branches or pull requests

2 participants