当前位置:首页 > CMS教程 > 其它CMS > 列表

laravel 根据不同组织加载不同视图的实现

发布:smiling 来源: PHP粉丝网  添加日期:2022-01-04 19:44:13 浏览: 评论:0 

今天小编就为大家分享一篇laravel 根据不同组织加载不同视图的实现,具有很好的参考价值,希望对大家有所帮助,一起跟随小编过来看看吧。

一,controller 层定义helper.php 文件

定义全局常量

  1. public function __construct() 
  2. { 
  3.   $this->middleware(function ($request, $next) { 
  4.     $this->_user = Auth::user(); 
  5.     //全局的数据处理,所有视图共用  
  6.     $this->_beforeActionInit(); 
  7.     if ($this->_user) { 
  8.       define('ORG_ID', $this->_user->organization_id); 
  9.       $this->_currentOrganization = Organization::find(ORG_ID); 
  10.     } else { 
  11.       define('ORG_ID', 0); 
  12.     } 
  13.       
  14.     View::share('user', $this->_user); 
  15.     View::share('currentOrganization', $this->_currentOrganization); 
  16.     return $next($request); 
  17.   }); 
  18. }
  19.    
  20. /** * 获取对应视图 */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 (emptyempty($view)) { throw new RuntimeException('Orgnization Error'); } return $view; }} 
  21.  
  22.  
  23. //二, config 下定义view.php 
  24. return [ 
  25.   'register' => [ 
  26.     1 => 'register.1', 
  27.     2 => 'register.2' 
  28.   ] 
  29. ] 
  30. // 三,sercive 层定义UserService.php 
  31. public function getValidateRule($org_id) 
  32. { 
  33.    
  34.     $rule = [//验证必填项,确认密码和密码要相同 
  35.       'userName' => 'required|alpha_num|size:6|regex:/^[a-zA-Z]{3}[0-9]{2}[a-zA-Z]{1}$/', 
  36.       'password' => 'required|min:6', 
  37.       'confirmPassword' => 'required|same:password', 
  38.     ]; 
  39.     
  40.   return $rule; 
  41. } 
  42. public function __construct() 
  43. { 
  44.   $this->middleware(function ($request, $next) { 
  45.     $this->_user = Auth::user(); 
  46.     //全局的数据处理,所有视图共用  
  47.     $this->_beforeActionInit(); 
  48.     if ($this->_user) { 
  49.       define('ORG_ID', $this->_user->organization_id); 
  50.       $this->_currentOrganization = Organization::find(ORG_ID); 
  51.     } else { 
  52.       define('ORG_ID', 0); 
  53.     } 
  54.      
  55.     View::share('user', $this->_user); 
  56.     View::share('currentOrganization', $this->_currentOrganization); 
  57.     return $next($request); 
  58.   }); 
  59. } 
  60.  
  61.  
  62.  
  63.  
  64. /** * 获取对应视图 */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 (emptyempty($view)) { throw new RuntimeException('Orgnization Error'); } return $view; }} 
  65.  
  66. //二, config 下定义view.php 
  67. return [ 
  68.   'register' => [ 
  69.     1 => 'register.1', 
  70.     2 => 'register.2' 
  71.   ] 
  72. ] 
  73. // 三,sercive 层定义UserService.php 
  74. public function getValidateRule($org_id) 
  75. { 
  76.  
  77.     $rule = [//验证必填项,确认密码和密码要相同 
  78.       'userName' => 'required|alpha_num|size:6|regex:/^[a-zA-Z]{3}[0-9]{2}[a-zA-Z]{1}$/', 
  79.       'password' => 'required|min:6', 
  80.       'confirmPassword' => 'required|same:password', 
  81.     ]; 
  82.    
  83.   return $rule; 
  84. } 

四,view下定义视图

register文件夹下有

1.blade.php,

2.blade.php

  1. //五,controller下引用 
  2.  
  3. /** 
  4.  * 注册 
  5.  */ 
  6. public function register(Request $request) 
  7. { 
  8.     
  9.     //提交注册 
  10.     if ($request->isMethod('post')) { 
  11.       $credentials = $request->only(['userName', 'password', 'confirmPassword']);//表单提交数据 
  12.       $rules = UserService::make($location->organization_id)->getValidateRule($location->organization_id); 
  13.       $validator = Validator::make($credentials, $rules); 
  14.       if ($validator->fails()) {//验证不通过 
  15.         return Redirect::back()->withInput()->withErrors($validator); 
  16.       } 
  17.       $exists = User::where('name', $credentials['userName'])->first(); 
  18.       if ($exists) { 
  19.         $result = Lang::has("register.userExists") ? trans("register.userExists") : "User exists"; 
  20.         return $this->_remind('error', $result, 'register'); 
  21.       } 
  22.       $user = new User(); 
  23.       $user->name = trim($credentials['userName']); 
  24.       $user->password = bcrypt($credentials['password']); 
  25.       if ($user->save()) { 
  26.         //注册成功 
  27.         return redirect('/login')->with('msg', Lang::has("register.success") ? trans("register.success") : 'Register Success.'); 
  28.       } else { 
  29.         //注册失败 
  30.         $validator->errors()->add('other', $user);//如果注册失败会把错误原因返回 
  31.         return Redirect::back()->withInput()->withErrors($validator); 
  32.       } 
  33.     } 
  34.     return view(get_organization_view('register',$organization_id), ["location" => $location->name]);//加载视图 
  35.   } catch (\Exception $ex){ 
  36.     $this->_remind('error', $ex->getMessage(),'getActivationCode'); 
  37.   } 
  38. }

Tags: laravel组织加载 laravel不同视图

分享到: