laravel 根據(jù)不同組織加載不同視圖的實現(xiàn)
更新時間:2019年10月14日 18:02:16 作者:路上的影子
今天小編就為大家分享一篇laravel 根據(jù)不同組織加載不同視圖的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
一,controller 層定義helper.php 文件
定義全局常量
public function __construct() { $this->middleware(function ($request, $next) { $this->_user = Auth::user(); //全局的數(shù)據(jù)處理,所有視圖共用 $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); }); } /** * 獲取對應(yīng)視圖 */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']);//表單提交數(shù)據(jù) $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'); } }
以上這篇laravel 根據(jù)不同組織加載不同視圖的實現(xiàn)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
jQuery Mobile + PHP實現(xiàn)文件上傳
這篇文章主要介紹了jQuery Mobile + PHP實現(xiàn)文件上傳的方法實例,以及由于自己疏忽造成的問題的解決方法,這里推薦給大家,有需要的小伙伴參考下2014-12-12在Yii2中使用Pjax導致Yii2內(nèi)聯(lián)腳本載入失敗的原因分析
這篇文章主要介紹了在Yii2中使用Pjax導致Yii2內(nèi)聯(lián)腳本載入失敗的原因分析的相關(guān)資料,需要的朋友可以參考下2016-03-03PHP基于自增數(shù)據(jù)如何生成不重復的隨機數(shù)示例
這篇文章主要給大家介紹了利用PHP基于自增數(shù)據(jù)如何能生成不重復的隨機數(shù),文中給出了詳細的示例代碼供大家參考學習,對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-05-05PHP實現(xiàn)基于狀態(tài)的責任鏈審批模式詳解
這篇文章主要介紹了PHP實現(xiàn)基于狀態(tài)的責任鏈審批模式,結(jié)合實例形式詳細分析了責任鏈審批模式的原理及相關(guān)php實現(xiàn)流程,需要的朋友可以參考下2019-05-05PHP實現(xiàn)簡單網(wǎng)站訪客統(tǒng)計的方法實例
這篇文章主要給大家介紹了關(guān)于PHP實現(xiàn)簡單網(wǎng)站訪客統(tǒng)計的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01