Goodnight. I am puzzled and can’t understand how I can implement multilingualism and switching between the admin panel and the regular version of the site.
I started writing my router, because I still don’t know how to find a common language with frameworks, I just can’t figure it out.
I really hope that there will be a person who will help me implement my version of routing. And so, I will describe the problem and the task:

I create a site. It is required to make multilingual (3 languages) and admin panel. Links should be of this type:
Regular version

  1. http://web-site.com/
    • http://web-site.com/catalog/auto/
    • http://web-site.com/catalog/auto/product/product-url/
    • http://web-site.com/
  2. http://web-site.com/ru/
    • http://web-site.com/ru/catalog/
    • http://web-site.com/ru/catalog/auto/
    • http://web-site.com/ru/catalog/auto/product/product-url/

If you add /admin/ in the address bar, then the redirect should work and connect the directory with the admin panel files.

  • http://web-site.com/admin/
  • http://web-site.com/admin/products/auto/

The .htaccess file contains the following:

 RewriteEngine on RewriteBase / RewriteCond %{REQUEST_FILENAME} !^favicon\.ico RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php?$1 [L,QSA] 

When I open the site, I want, say, the default Russian language, that is, a link like http://web-site.com/ is Russian, if the user chooses another language, then all links that are on the site are automatically formatted into specified locale, that is:
http://web-site.com/catalog/ formatted in http://web-site.com/en/catalog/ . I have no idea how to convert all the links for the desired locale using php.

Then my router:

 class Router { public $URI = []; public $TEMPLATE; public $MODULE = []; public $LOCALE; function __construct() { self::parseQueryString(); } function parseQueryString() { /** * Метод который парсит строку запроса, и возвращает массив $URI **/ $parsedString = parse_url($_SERVER['QUERY_STRING'], PHP_URL_PATH); $parsedString = explode('/', $parsedString); $this->URI = $parsedString; return $this->URI; } function setTemplate() { /** * Этот метод должен запустить проверку на существование шаблона * если таков есть, то возвращает название шаблона, если нет * то название шаблона ошибки **/ $template = $this->TEMPLATE; if (!$this->checkTemplate($template, $module)) $template = 'error'; return $template; } function checkTemplate($template, $module) { /** * Этот метод проверяет наличие файла (шаблона) * Возвращает TRUE | FALSE **/ $checkPath = $_SERVER['DOCUMENT_ROOT'] . '/application/views/' . $module . '/'; $checked = false; if (file_exists($checkPath . $template . '.php')) $checked = true; return $checked; } static function redirect($path = NULL) { /** * Статический метод обычной перезагрузки. * Использую после отправки формы методом пост, * либо когда это нужно **/ if (!$path) { $redirect = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : $_SERVER['REQUEST_URI']; header("Location: $redirect"); } else header("Location: /$path"); exit; } } 

It is not finished yet, for this reason I came to you. I hope someone will help finish or explain how to do it.

First of all, the router must accept and parse the address bar. I did this.
But now I need to define and format links automatically under the desired locale. For example, the user comes to the site:
http://web-site.com/ gets to the main page with the Russian language. Then go to the product catalog http://web-site.com/catalog/ and switch to English, I need to redirect the user to http://web-site.com/en/catalog/ and in the router accept this /en/ and pass it to the $LOCALE variable, which I can then use to display the necessary information in the correct locale.
If in the address bar the user enters http://web-site.com/admin/ then he should be transferred to the authorization page of the admin panel. But I do not quite understand how to do this.
I have the following folder structure:

  • web-site
    • application
      • configs
        • config.php
      • controller
        • controller.php
      • core [dir]
        • Router.php
        • Database.php
      • models [dir]
        • CatalogModel.php
        • NewsModel.php
        • etc...
      • views
        • web_site [ types of site pages for a regular user ]
        • control_panel [ types of admin panel pages ]
      • loader.php
    • css
    • img
    • js
    • uploads
    • index.php

File loader.php all the necessary files at the very beginning.
Then I launch my router in the controller.php file.
Unfortunately, I don’t know where to go or what to do next. If you need more information, I will provide everything you need.

    1 answer 1

    In the structure described by you, it is not quite clear to me why your router is connected in the controller. That is, as I understand the sequence is

    index.php -> loader.php -> controller.php -> router.php

    What is the controller responsible for in this scheme? Just connecting the router?

    I think the general approach can be organized this way:

    index.php -> loader.php -> router.php -> controller.php

    Where controller.php is the controller of each individual page, that is, CatalogController, ProductController.

    The second moment Why does the router have a template connection? The main and only task of the router is to parse the url and connect the controller. And you need to connect the necessary templates in the controller. To do this, you can make an abstract Controller class that implements a method, for example, render, which will include the desired template.

    Multilingual In general, the solution is as follows:

    Parse the url and if after the address of the site something similar to the locale is found, we load the necessary language file, and if not found we load the standard one.

    In the template, all the static information output is obtained through a function that by code gets the desired text from the language file.

    • Thanks for the answer. But the variant you offer is exactly the same used in MVC patterns, in most frameworks. I can not quite understand how to properly use it. But perhaps I’ll try to do something good now, since no one else answered - Rosnowsky
    • So why not use this option? - Victor
    • I cannot understand how and where to apply, say, the methods that extract information from the database on each of the pages (header, footer, sidebars), a few more things that I don’t understand at all in this pattern) - Rosnowsky
    • So ask specific questions. Above, I described the boot algorithm to the controller. No questions on it? For each page you create a controller inherited from the main one - ContactController, CategoryController, etc. Each controller is essentially a separate page that contains logic, receiving data from models, connecting a template. That is, inside the controller you receive data from the models, change them, connect the template and transfer this data there - Victor