1. 程式人生 > >laravel 根據不同組織載入不同檢視

laravel 根據不同組織載入不同檢視

一,controller 層定義helper.php 檔案

定義全域性常量
public function __construct()
{
$this->middleware(function ($request, $next) {
$this->_user = Auth::user();
//全域性的資料處理,所有檢視共用 
$this->_beforeActionInit();
if ($this->_user) {
            define('ORG_ID', $this->_user->organization_id);
$this->
_currentOrganization = Organization::find(ORG_ID); } else { define('ORG_ID', 0); } View::share('user', $this->_user); View::share('currentOrganization', $this->_currentOrganization); return $next($request); }); }




/** * 獲取對應檢視 */if (!function_exists('get_organization_view')) {/** * @param
$flag * @return \Illuminate\Config\Repository|mixed */function get_organization_view($flag, $org_id = 1) {$view = config("view.$flag." . $org_id);if (empty($view)) {throw new RuntimeException('Orgnization Error');}return $view;}}


//二, config 下定義view.php
return [
'register' => [
1 => 'register.1'
, 2 => 'register.2' ] ] // 三,sercive 層定義UserService.php
public function getValidateRule($org_id)
{
$rule = [//驗證必填項,確認密碼和密碼要相同
'userName' => 'required|alpha_num|size:6|regex:/^[a-zA-Z]{3}[0-9]{2}[a-zA-Z]{1}$/',
'password' => 'required|min:6',
'confirmPassword' => 'required|same:password',
];
return $rule;
}
四,view下定義檢視
register資料夾下有1.blade.php,
2.blade.php


//四,controller下引用

/**
 * 註冊
 */
public function register(Request $request)
{
//提交註冊
if ($request->isMethod('post')) {
$credentials = $request->only(['userName', 'password', 'confirmPassword']);//表單提交資料
$rules = UserService::make($location->organization_id)->getValidateRule($location->organization_id);
$validator = Validator::make($credentials, $rules);
if ($validator->fails()) {//驗證不通過
return Redirect::back()->withInput()->withErrors($validator);
}
$exists = User::where('name', $credentials['userName'])->first();
if ($exists) {
$result = Lang::has("register.userExists") ? trans("register.userExists") : "User exists";
return $this->_remind('error', $result, 'register');
}
$user = new User();
$user->name = trim($credentials['userName']);$user->password = bcrypt($credentials['password']);if ($user->save()) {
//註冊成功
return redirect('/login')->with('msg', Lang::has("register.success") ? trans("register.success") : 'Register Success.');
} else {
//註冊失敗
$validator->errors()->add('other', $user);//如果註冊失敗會把錯誤原因返回
return Redirect::back()->withInput()->withErrors($validator);
}
        }
return view(get_organization_view('register',$organization_id), ["location" => $location->name]);//載入檢視
} catch (\Exception $ex){
$this->_remind('error', $ex->getMessage(),'getActivationCode');
}
}