laravel 根據(jù)不同組織加載不同視圖的實(shí)現(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); }); } /** * 獲取對(duì)應(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 = [//驗(yàn)證必填項(xiàng),確認(rèn)密碼和密碼要相同 '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下引用 /** * 注冊(cè) */ public function register(Request $request) { //提交注冊(cè) 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()) {//驗(yàn)證不通過(guò) 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()) { //注冊(cè)成功 return redirect('/login')->with('msg', Lang::has("register.success") ? trans("register.success") : 'Register Success.'); } else { //注冊(cè)失敗 $validator->errors()->add('other', $user);//如果注冊(cè)失敗會(huì)把錯(cuò)誤原因返回 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ù)不同組織加載不同視圖的實(shí)現(xiàn)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
jQuery Mobile + PHP實(shí)現(xiàn)文件上傳
這篇文章主要介紹了jQuery Mobile + PHP實(shí)現(xiàn)文件上傳的方法實(shí)例,以及由于自己疏忽造成的問(wèn)題的解決方法,這里推薦給大家,有需要的小伙伴參考下2014-12-12在Yii2中使用Pjax導(dǎo)致Yii2內(nèi)聯(lián)腳本載入失敗的原因分析
這篇文章主要介紹了在Yii2中使用Pjax導(dǎo)致Yii2內(nèi)聯(lián)腳本載入失敗的原因分析的相關(guān)資料,需要的朋友可以參考下2016-03-03PHP基于自增數(shù)據(jù)如何生成不重復(fù)的隨機(jī)數(shù)示例
這篇文章主要給大家介紹了利用PHP基于自增數(shù)據(jù)如何能生成不重復(fù)的隨機(jī)數(shù),文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-05-05Symfony2學(xué)習(xí)筆記之系統(tǒng)路由詳解
這篇文章主要介紹了Symfony2系統(tǒng)路由,詳細(xì)分析了Symfony路由原理及路由的創(chuàng)建,配置與使用技巧,需要的朋友可以參考下2016-03-03Yii2框架實(shí)現(xiàn)登陸添加驗(yàn)證碼功能示例
這篇文章主要介紹了Yii2框架實(shí)現(xiàn)登陸添加驗(yàn)證碼功能,結(jié)合實(shí)例形式分析了Yii2框架登陸添加驗(yàn)證碼相關(guān)的設(shè)置、控制器及視圖操作技巧,需要的朋友可以參考下2018-07-07PHP實(shí)現(xiàn)基于狀態(tài)的責(zé)任鏈審批模式詳解
這篇文章主要介紹了PHP實(shí)現(xiàn)基于狀態(tài)的責(zé)任鏈審批模式,結(jié)合實(shí)例形式詳細(xì)分析了責(zé)任鏈審批模式的原理及相關(guān)php實(shí)現(xiàn)流程,需要的朋友可以參考下2019-05-05PHP實(shí)現(xiàn)簡(jiǎn)單網(wǎng)站訪客統(tǒng)計(jì)的方法實(shí)例
這篇文章主要給大家介紹了關(guān)于PHP實(shí)現(xiàn)簡(jiǎn)單網(wǎng)站訪客統(tǒng)計(jì)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01