If you like DNray Forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...

 

One website for 3-Level domains

Started by maryse, Jan 06, 2023, 09:53 AM

Previous topic - Next topic

maryseTopic starter

Could you explain how to create similar functionality?

An example of a website with such functionality is schools.by. This website allows the creation of sub-sites for schools, each having a 3rd-level domain like school.schools.by. It is not feasible to create a separate website for every school, so the application handles requests from level 3 domains.

Queries on the sub-sites are structured in the following format: schools.by/teacher/id instead of schools.by/school/teacher/id. I am developing a portal with a similar architecture where information is located on the main 2nd level domain and there are sub-sites with the same functionality on 3rd level domains.

If you need guidance on implementing this functionality on Yii2, here are some working examples:
- https://schools.by/sites (main site)
- https://upk1.schools.by/ (sub-site)
  •  


kumarajite

How to configure the server to route all requests to yii's standard entry point within index.php, including requests from subdomains?

There are two options available. The first option is to use Rules with Server Names, which works well when each subdomain has different controllers. However, in cases where the subdomains share the same controllers and actions, but require a unique school_id, an alias can be used instead. To implement this, add an alias in common\config\bootstrap.php by setting Yii::setAlias('@school', $_SERVER['SERVER_NAME']). Please note that checking the $_SERVER['SERVER_NAME'] value is important, as it may vary depending on the server settings.

For example, we can use the following code to retrieve Prepod:
public function getPrepod(){
  return $this->hasOne(Prepod:classname, ['id'=>'prepod_id'])->andWhere(['school_id'=>Yii::getAlias('@school')]);
}
  •  

James Fisher

Creating a similar functionality to schools.by on Yii2 would involve setting up a multi-tenant architecture. Each sub-site for the schools would be a separate instance of the application, sharing the same codebase but with its own database and configuration.

To achieve this, you would need to use Yii2's advanced application template, which provides a structure for handling multiple applications within the same codebase. Each school's sub-site would correspond to a separate frontend or backend application within the Yii2 advanced template.

For the domain routing, you can use Yii2's URL manager to handle requests from third-level domains and map them to the appropriate controllers and actions. This would allow you to structure URLs like school.schools.by/teacher/id while internally handling them as schools.by/school/teacher/id.

Additionally, you would need to configure the database connections to support multi-tenancy, ensuring that each school's data is isolated and belongs only to that specific sub-site.

By following these guidelines and utilizing Yii2's features, you can create a scalable and efficient system similar to schools.by, allowing for the management of multiple sub-sites with ease.


Some code examples for implementing similar functionality to schools.by on Yii2. Let's start with the domain routing and database multi-tenancy.

1. **Domain Routing**:

// Configuring URL Manager in main.php

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules' => [
        '<school:w+>.schools.by/<controller:w+>/<action:w+>' => '<school>/<controller>/<action>',
        // Other rules for main site
    ],
],


In this example, we configure the URL manager to handle requests from third-level domains like school.schools.by and map them to the appropriate controllers and actions.

2. **Database Multi-Tenancy**:

// Configuring DB component in main.php

'db' => [
    'class' => 'yiidbConnection',
    'dsn' => 'mysql:host=localhost;dbname=common_db',
    'username' => 'common_user',
    'password' => 'common_password',
    // Other common config options
],


For multi-tenancy, you can use a single common database for shared data and a separate database or schema for each school's sub-site. You can dynamically switch the database connection based on the requested subdomain using Yii2's event handling mechanisms.

These are just simplified examples to illustrate the concepts. In a real-world scenario, you would need to implement more advanced logic for handling multi-tenancy and domain routing, such as dynamically configuring database connections based on subdomain, ensuring data isolation, and providing a robust configuration system for managing sub-sites.
  •  


If you like DNray forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...