Multi-application (multi-backend)

The default installation is in single-application mode, if you want to use multi-application mode in the same laravel project. Then you can use a multi-backend model, and the directory structure in the final project would be as follows

  1. app
  2. ├──Admin
  3. ├── Controllers
  4. ├── ExampleController.php
  5. └── HomeController.php
  6. ├── Metrics
  7. └── ...
  8. ├── bootstrap.php
  9. └── routes.php
  10. ├──Admin2
  11. └── ...
  12. │──Admin3
  13. └── ...
  14. ...

Generate new applications

Run command, this command only accepts one parameter: application name, please be sure to use PascalCase for the application name.

  1. php artisan admin:app NewAdmin

After running success a new application directory app/NewAdmin will be added to your project, as well as a new configuration file config/new-admin.php.

  1. app
  2. └──Admin
  3. ├── Controllers
  4. ├── ExampleController.php
  5. └── HomeController.php
  6. ├── Metrics
  7. └── ...
  8. ├── bootstrap.php
  9. └── routes.php
  10. config
  11. └──new-admin.php

Enable

Once the new application has been generated, you can start enabling the new application by opening the configuration file config/admin.php and adding the following code

  1. return [
  2. ...
  3. 'multi_app' => [
  4. // Consistent with the new application's profile name
  5. // set to true to enable, false to disable.
  6. 'new-admin' => true,
  7. ],
  8. ];

Then you can open your browser to access the new app http://localhost:8000/new-admin

Change Route Prefix

Currently only through the route prefix to distinguish between different applications, if you want to change the application’s prefix, you can open the configuration file new-admin.php to find the route.prefix parameter to change it!

Change the menu

If you want to display a different menu in your new app, you can refer to the following methods

  1. First you need to create a new menu table and its associated tables ``sql CREATE TABLEnew_admin_menu(idint(10) unsigned NOT NULL AUTO_INCREMENT,parent_idint(11) NOT NULL DEFAULT '0',orderint(11) NOT NULL DEFAULT '0',titlevarchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,iconvarchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,urivarchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,created_attimestamp NULL DEFAULT NULL,updated_attimestamp NULL DEFAULT NULL, PRIMARY KEY (id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE new_admin_permission_menu ( permission_id int(11) NOT NULL, menu_id int(11) NOT NULL, created_at timestamp NULL DEFAULT NULL, updated_at timestamp NULL DEFAULT NULL, UNIQUE KEY admin_permission_menu_permission_id_menu_id_index (permission_id,menu_id) USING BTREE ) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE new_admin_role_permissions ( role_id int(11) NOT NULL, permission_id int(11) NOT NULL, created_at timestamp NULL DEFAULT NULL, updated_at timestamp NULL DEFAULT NULL, UNIQUE KEY admin_role_permissions_role_id_permission_id_index (role_id,permission_id) USING BTREE ) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

  1. 2.Create a new menu model, and note that the default menu model needs to be inherited here!!!
  2. ```php
  3. <?php
  4. namespace App\Models;
  5. use Dcat\Admin\Models\Menu;
  6. class NewMenu extends Menu
  7. {
  8. protected $table = 'new_admin_menu';
  9. }

3.Open the new application’s configuration file config/new-admin.php and modify the following parameters

  1. return [
  2. ...
  3. 'database' => [
  4. ...
  5. // Writing a new model and menu table
  6. 'menu_table' => 'new_admin_menu',
  7. 'menu_model' => App\Models\NewMenu::class,
  8. ...
  9. // New intermediate table
  10. 'role_menu_table' => 'new_admin_role_menu',
  11. 'permission_menu_table' => 'new_admin_permission_menu',
  12. ],
  13. ];

So that new applications can use the separate menu function

Changing users and permissions

For custom users and permissions, you can refer to the way to change the menu above. In addition, if you are a custom user, you need to change the following parameters in the configuration file config/new-admin.php.

  1. ...
  2. 'auth' => [
  3. ...
  4. 'guard' => 'new-admin', // It has to be a new name.
  5. 'guards' => [
  6. 'new-admin' => [
  7. 'driver' => 'session',
  8. 'provider' => 'new-admin', // It has to be a new name.
  9. ],
  10. ],
  11. 'providers' => [
  12. 'new-admin' => [ // It has to be a new name.
  13. 'driver' => 'eloquent',
  14. // Here's the new user table model instead
  15. 'model' => App\Models\NewAdministrator::class,
  16. ],
  17. ],
  18. ...
  19. ],

Use different domain names to distinguish applications

By default, applications are distinguished by routing prefixes. If you want to distinguish applications using domain names, you just need to change the following configuration

  1. 'route' => [
  2. 'domain' => 'dev.dcat.com', // configure your domain name
  3. 'prefix' => '', // the route prefix is recommended to be set to empty
  4. 'namespace' => 'App\\Admin\\Controllers',
  5. 'middleware' => ['web', 'admin'],
  6. ],