使用laravel HYN多租户包创建租户后,如何切换回系统环境?

问题描述 投票:0回答:1

我正在开发一个多租户的laravel应用,使用的是 HYN包.

到目前为止,一切都很好,但每当我通过系统创建一个租户时,我必须切换到该租户的环境,以将一个超级管理员用户播种到该特定的数据库中。

请看下面我是如何从系统端创建一个租户的。

SchoolController.php

public function createSchool(Request $request)
    {

        $validator = Validator::make($request->all(),[
            'school_name' => 'required',
            'school_address' => 'required',
            'school_logo' => 'required|file',
            'school_type' => 'required',
            'school_sub_domain' => 'required',
            'school_email' => 'required',
            'school_default_password' => 'required',
            'school_modules' => 'required|array'
        ]);

        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator)->withInput();
        }


        $website = new Website;
        $website->school_name = $request->school_name;
        $website->school_address = $request->school_address;
        //$website->school_logo = $request->school_logo;
        $website->school_type = $request->school_type;
        $website->school_email = $request->school_email;
        $website->school_default_password = $request->school_default_password;

        $school_logo = $request->file('school_logo');
        $allowed_file_extentions = [
            'jpeg',
            'png',
            'jpg',
            'gif',
            'svg'
        ];

        if (false == in_array($school_logo->getClientOriginalExtension(), $allowed_file_extentions)) {
            return back()->withErrors(['school_logo'=>'Allowed file types are jpeg,png,jpg,gif and svg']);
        }

        $website->school_logo = 'NOT_SET';

        app(WebsiteRepository::class)->create($website);

        $logo_path = $website->uuid.'/logo/' . time() . '.' . $school_logo->getClientOriginalExtension();

        $t = Storage::disk('tenant')->put($logo_path, file_get_contents($school_logo), 'public');

        $website->school_logo = $logo_path;
        $website->save();


        //creating hostname
        $hostname = new Hostname;
        $hostname->fqdn = $request->school_sub_domain.'.'.config('app.app_fqdn');

        app(HostnameRepository::class)->create($hostname);
        app(HostnameRepository::class)->attach($hostname, $website);


        $school_modules = $request->school_modules;
        $school_modules_to_create = [];

        foreach($school_modules as $key => $value){
            array_push($school_modules_to_create,[
                'website_id' => $website->id,
                'module_id' => $value
            ]);
        }

        WebsiteModule::insert($school_modules_to_create); // System model

        //Switching to tenant environment to seed super admin for the tenant

        $tenancy = app(Environment::class);
        $tenancy->hostname($hostname);
        $tenancy->tenant($website);
        $tenancy->tenant(); // resolves $website

        $id = Staff::createSuperAdminFromSystem([
            'email' => $request->school_email,
            'password' => $request->school_default_password,
        ]);

        return redirect()->route('system.add_module'); //Here I am getting issue.
    }

web.php

Route::prefix('system')->group(function () {
    Route::get('login', ['as' => 'system.login', 'uses' => 'System\AuthController@viewLogin']);
    Route::post('login-validate', ['as' => 'system.login.validate', 'uses' => 'System\AuthController@loginValidate']);

    Route::group(['middleware' => ['system.auth']], function () {
        Route::get('dashboard', ['as' => 'system.dashboard', 'uses' => 'System\AuthController@viewDashboard']);

        Route::get('logout', ['as' => 'system.logout', 'uses' => 'System\AuthController@doLogout']);

        ########### Sidebar routes ###############
        Route::get('modules', ['as' => 'system.modules', 'uses' => 'System\ModuleController@viewModules']);

        Route::get('add-module', ['as' => 'system.add_module', 'uses' => 'System\ModuleController@viewAddModule']);
        Route::post('create-module', ['as' => 'system.create_module', 'uses' => 'System\ModuleController@createModule']);

        Route::get('add-school',['as' => 'add.school', 'uses' => 'System\SchoolController@addSchool']);
        Route::post('create-school',['as' => 'system.create.school', 'uses' => 'System\SchoolController@createSchool']);
    });
});

如你所见 route('system.add_module') 是系统路由,假设我的系统基本网址是 http://example.com 因此我应该被重定向到 http://example.com/system/add-module 但我被引导到 http://<tenant_hostname>.example.com/system/add-module

请在这方面帮助我,因为 文件 颇为含糊,难以理解。

php laravel multi-tenant
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.