一,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è)參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:- Laravel基礎(chǔ)_關(guān)于view共享數(shù)據(jù)的示例講解
- laravel框架模型、視圖與控制器簡(jiǎn)單操作示例
- laravel通過(guò)a標(biāo)簽從視圖向控制器實(shí)現(xiàn)傳值
- Laravel5.5 視圖 - 創(chuàng)建視圖和數(shù)據(jù)傳遞示例