摘要:學(xué)習(xí)了一段時間的小結(jié)一下最近做的小任務(wù)寫下來才知道好亂糟糟,還是以記錄學(xué)習(xí)的資料為主,寫的很糟糕,還需要再揣度多屢屢思路。
序學(xué)習(xí)了一段時間的laravel,小結(jié)一下最近做的laravel小任務(wù),寫下來才知道好亂糟糟,還是以記錄學(xué)習(xí)的資料為主,寫的很糟糕,還需要再揣度多屢屢思路。
20151103-16源碼地址:https://github.com/dingyiming/xc-addinfo-1103
學(xué)習(xí)資料:
laravel學(xué)院
laravist.com
laracasts.com
laravelcollective
vuejs
預(yù)先準備
關(guān)節(jié)點
登錄
用戶信息增改刪查與權(quán)限區(qū)分
員工管理與權(quán)限區(qū)分
字段查詢
修改個人密碼
前端vue-form驗證
dingo/API + vue-resource 唯一性判斷
添加Oauth2.0
拓展
預(yù)先準備學(xué)習(xí)了laravel
創(chuàng)建項目 :打開終端,新建文件夾mkdir dev,下載laravel項目源碼
composer create-project laravel/laravel addinfo
php artisan server 訪問localhost:8000查看
配置好.env數(shù)據(jù)庫配置
在我學(xué)了一陣子的laravel后,感覺到有很多東西都是了解到,但卻不清晰,然后接到這個任務(wù)我開始著手實踐一下,自然沒想象的那么順利,其中遇到了很多看起來很基礎(chǔ)的問題,往往會卡我半天甚至是一天,真的蠻郁悶的,在自己的草稿本上多少有些殘留,回顧一下,很有收獲,其中得到了laravel學(xué)院和Laravist.com很大的幫助,非常感謝;
關(guān)節(jié)點剛剛開始的時候,我恰巧看了Laravist.com關(guān)于API的那一個系列教程(60元),我按著他得路子做了一些開頭,我其實是很想用vuejs做成SPA的,不過自己還不會做,還是應(yīng)該先以完成任務(wù)為目標,一步步改進自己,所以還是使用模板Blade來渲染視圖。
我先用bootstrap做了下頁面,也由于一些原因,還沒用上SCSS,希望后面有時間可以嘗試改進一下,在此就不放頁面樣式了,主要做些關(guān)鍵點的記錄,現(xiàn)在想想還是一團糟的,得抓重點回憶。
我覺著在往常的web應(yīng)用中主要是這幾個關(guān)節(jié)點:
1.數(shù)據(jù)庫連接配置: .env
2.路由 : (訪問的URL)從哪里去找誰(一般找控制器) http://laravelacademy.org/post/53.html
3.控制器 : 寫邏輯操作和少量的數(shù)據(jù)驗證與數(shù)據(jù)操作(Eloquent很快捷)http://laravelacademy.org/post/60.html
4.數(shù)據(jù)處理: 輔助控制器進行一些數(shù)據(jù)處理與快捷映射,
模型類 : app/Http/Model
請求驗證Requests : app/Http/Requests
數(shù)據(jù)倉庫Repositories: app/Http/Repositories (laravel不自帶)
數(shù)據(jù)倉庫Repositories配置方法:http://segmentfault.com/a/1190000003488038
5.把結(jié)果數(shù)據(jù)傳遞給視圖:視圖渲染頁面(blade) ,視圖基本上都是使用blade和 IlluminateHtml
靜態(tài)資源的引用:/assets/css/all.css
參考資料:
http://laravelacademy.org/post/76.html
http://laravelacademy.org/post/79.html
https://laravist.com/article/14
http://laravelcollective.com/docs/5.1/html
6.在laravel重要的ServiceProvider在config/app.php里配置后可在全局快捷使用;
7.消息反饋 :參考:http://laravelacademy.org/post/68.html
在控制器中使用larvel一次性Session:
$request->session()->flash("status1", $msg1);
在視圖中判斷顯示
@if(session("status1"))登錄OK! {{session("status1")}}@endif
我把用戶信息的增改刪查作為resource路由,接著是因為用戶信息的操作是需要用戶登錄才操作的,所以,我跟著laravel學(xué)院的教程做了下laravel內(nèi)置的auth注冊登錄;
使用laravel內(nèi)置 auth,
參考資料:http://laravelacademy.org/post/1258.html
laravel自帶了開箱即用的auth注冊登錄(只需要配置一下路由和視圖),已有用戶模型AppUser,控制器在appHttpControllersAuthAuthController.php
默認是使用email進行注冊登錄,可以在AuthController.php里進行修改一些驗證以及默認的登錄字段 protected $username = "name";
自動生成表 php artisan migrate 數(shù)據(jù)庫出現(xiàn)三張表
添加登錄路由,在appHttp outes.php
創(chuàng)建視圖 在resources/views/auth/login.blade.php
表單如果是自己寫得HTML,需要加上隱藏的csrf_tokern,
在blade中form下輸入 {!! csrf_field()!!}即可;
修改登錄成功后的跳轉(zhuǎn)地址,
在appHttpControllersAuthAuthController.php中添加protected $redirectPath = "/userinfo";
現(xiàn)在可以正常注冊登錄了,然而我在這里遇到個坑,是關(guān)于密碼加密的,如果改了加密方法卻沒改校驗方法,就永久登錄失敗(注冊成功后會自動登錄,退出后將不能登錄,報錯信息為:These credentials do not match our records)
laravel在auth中使用了 "password" => bcrypt($data["password"]) 即使用了內(nèi)置的bcrypt()方法加密,這是不可逆的Hash加密,并且我自己還比對了下,發(fā)現(xiàn)每次相同字符串生成的密文還是不同的,如果需要驗證這個加密,就使用Hash::check($input, $oldpwd)驗證,返回值為true/false
顯示錯誤
@if (count($errors) > 0) //此處添加錯誤反饋, //例: swal("對不起", "xxx", "error");//sweetalert不過有點過, //可以使用bootstrap的alert @endif用戶信息增改刪查與權(quán)限區(qū)分
權(quán)限區(qū)分 http://laravelacademy.org/post/577.html
我在表中添加了用于區(qū)分權(quán)限的字段,在app/Providers/AuthServiceProvider.php中進行權(quán)限分配:
public function boot(GateContract $gate) { parent::registerPolicies($gate); //權(quán)限1,可以查看全部錄入的信息,以及所有員工 $gate->define("see-all", function ($user) { return $user->authority === 1; }); //權(quán)限2,可以查看部門提交的數(shù)據(jù) $gate->define("see-dep", function ($user) { return $user->authority === 2; }); //權(quán)限3,可以查看自己提交的數(shù)據(jù) $gate->define("see-me", function ($user) { return $user->authority === 3; }); }
路由
//登錄用戶才能訪問進行用戶信息操作 Route::group(["middleware" => "auth"], function () { resource("userinfo", "UserinfosController"); });
Model設(shè)置,對應(yīng)表、可填充字段、表關(guān)聯(lián)
資料:http://laravelacademy.org/post/140.html
逆向的遠層一對多:https://github.com/znck/belongs-to-through
protected $table = "userinfos"; protected $fillable = [ "phone", "name", "email", ]; //多個信息對應(yīng)一個錄入信息的人 public function user() { return $this->belongsTo("AppUser", "addman_id", "id"); } //格式化生日時間 public function setBirthdayAttribute($birthday) { return $this->attributes["birthday"] = Carbon::createFromFormat("Y-m-d", $birthday); } //userinfo按照用戶ID倒序 public function scopeOrdered($query) { $query->OrderBy("userinfos.id", "desc"); }
控制器
//分權(quán)限顯示用戶信息 public function index(Request $req) { $datas = null; switch ($req) { case $req->user()->can("see-all"): $datas = $this->userinfos->selectAll(); break; case $req->user()->can("see-dep"): $datas = $this->userinfos->selectDep($this->user["dep_id"]); break; case $req->user()->can("see-me"): $datas = $this->userinfos->selectMe($this->user["id"]); break; } if ($datas) return view("userinfo.index", compact("datas")); return $this->responseResult(null, $req, "查詢失敗", null, "userinfo"); }
自建的數(shù)據(jù)倉庫 App/Repositories/UserinfoRepository.php
public function selectAll() { return Userinfo::ordered()->Paginate(env("PAGE_ROWS")); } public function selectDep($dep_id) { return Department::find($dep_id) ->userinfos() ->ordered() ->Paginate(env("PAGE_ROWS")); } public function selectMe($user_id) { return User::find($user_id) ->userinfos() ->ordered() ->Paginate(env("PAGE_ROWS")); }
視圖,省略。。。
基于權(quán)限的員工管理上面主要記錄了分權(quán)限進行的錄入信息查看,其它增改刪都會基于這里,具體看源碼
routes.php
//員工管理 Route::group(["middleware" => "auth"], function () { resource("users", "UsersController"); });
Model :app/User.php
protected $table = "users"; protected $fillable = ["email", "password", "realname", "dep_id", "authority"]; protected $hidden = ["password", "remember_token"]; //一個錄入信息的人對應(yīng)多個錄入的信息 public function userinfos() { return $this->hasMany("AppUserinfo", "addman_id", "id"); } //一個員工屬于一個部門 public function dep() { return $this->belongsTo("AppDepartment", "dep_id", "id"); }
控制器 UsersController.php
//顯示員工管理頁面 public function index(Request $request) { switch ($request) { case $request->user()->can("see-all"): $users = $this->users->getAllUser(); break; case $request->user()->can("see-dep"): $users = $this->users->getDepUser($this->auth["dep_id"]); break; default : return $this->responseResult(null, $request, "你沒有權(quán)限", "", "userinfo"); } return view("user.index", compact("users")); }
UserRepository.php
class UserRepository implements UserRepositoryInterface { public function getAllUser() { return $this->alluser()->Paginate(env("PAGE_ROWS")); } public function getDepUser($dep_id) { $users = $this->alluser(); return $users->where("dep_id", $dep_id)->Paginate(env("PAGE_ROWS")); } private function alluser() { $users = User::with("dep") ->select("users.*", "departments.dep_name") ->leftJoin("departments", "departments.id", "=", "users.dep_id") ->OrderBy("dep_id"); return $users; } }字段查詢
->where($field, "like", "%" . $data . "%")
//搜索姓名/手機/身份證 public function search(Request $req) { $name = $req->input("name"); $phone = $req->input("phone"); $identity = $req->input("identity"); switch (true) { case !empty($name): $datas = $this->userinfos->search($req, "name", $name); break; case !empty($phone): $datas = $this->userinfos->search($req, "phone", $phone); break; case !empty($identity): $datas = $this->userinfos->search($req, "identity", $identity); break; default: return $this->responseResult(null, $req, "請?zhí)顚懖樵儣l件", "", "userinfo"); } if ($datas->total() > 0) return view("userinfo.index", compact("datas")); return $this->responseResult(null, $req, "查詢不到你要的內(nèi)容", "", "userinfo"); }
對搜索也限定了權(quán)限
public function search($req, $field, $data) { switch ($req) { case $req->user()->can("see-all"): return $this->commonWhere($field, $data) ->Paginate(env("PAGE_ROWS")); break; case $req->user()->can("see-dep"): $dep_id = $req->user()["dep_id"]; return Department::findOrFail($dep_id) ->userinfos() ->where($field, "like", "%" . $data . "%") ->ordered() ->Paginate(env("PAGE_ROWS")); break; case $req->user()->can("see-me"): return $this->commonWhere($field, $data) ->where("addman_id", $req->user()["id"]) ->Paginate(env("PAGE_ROWS")); break; } }修改個人密碼
關(guān)鍵也就是前面在登錄提到的加密和驗證密碼的問題,用了laravel自帶方法:bcrypt()(即Hash::make())和Hash::check("輸入的老密碼","原密碼")
//更改密碼 public function updatereset(Request $request) { $this->validate($request, [ "old_password" => "required", "new_password" => "required | confirmed", ]); $user = User::find($this->auth["id"]); $input = $request->all(); $old_pwd0 = $user["password"]; $old_pwd1 = $input["old_password"]; if (Hash::check($old_pwd1, $old_pwd0)) { $user->password = Hash::make($input["new_password"]); $res = $user->save(); return $this->responseResult($res, $request, "修改失敗", "修改成功", "/userinfo"); } return $this->responseResult(null, $request, "原密碼不正確", "", "/users/resetpwd"); }前端vue-form驗證
vuejs官網(wǎng) : http://cn.vuejs.org
vuejs: https://github.com/vuejs/vue
vue-form : https://github.com/fergaldoyle/vue-form
頁面需要添加一些標識
js代碼
new Vue({ el: "#app", data: { myform: {}, model: {} }, methods: { onSubmit: function() { console.log(this.myform.$valid); if(this.myform.$valid == true) $("#myform").submit(); } } });dingo/API + vue-resource 唯一性判斷
dingo/API : https://github.com/dingo/api
vue-resource : https://github.com/vuejs/vue-resource
composer.json
"require": { "php": ">=5.5.9", "laravel/framework": "5.1.*", "illuminate/html": "^5.0", "lucadegasperi/oauth2-server-laravel": "5.0.*", "dingo/api": "1.0.*@dev" },
app.php配置
//dingo/api DingoApiProviderLaravelServiceProvider::class
.env配置
API_STANDARDS_TREE=vnd API_PREFIX=api API_VERSION=v1 API_DEBUG=true
路由
//dingo/api $api = app("DingoApiRoutingRouter"); $api->version("v1", function ($api) { $api->group(["namespace" => "AppApiControllers"], function ($api) { $api->get("onephone/{params}", "ValidController@onephone"); $api->get("oneidentity/{params}", "ValidController@oneidentity"); }); });
app/Api/Controllers/BaseController.php
class BaseController extends Controller { use Helpers;//使用Dingo內(nèi)置幫助函數(shù) }
其它Controller繼承BaseController.php,從而使用Dingo內(nèi)置幫助函數(shù)
vue-resource https://github.com/vuejs/vue-resource
onephone: function () { this.$http.get("/api/onephone/" + this.model.phone.trim(), function (data, status, request) { if (data == 0) { this.model.onephone = false; } if (data == 1) { console.log(data); this.model.onephone = true; } }).error(function (data, status, request) { }); },添加Oauth2.0
laravel-Oauth2.0 :https://github.com/lucadegasperi/oauth2-server-laravel
配置
"providers" => [ //Oauth2.0 LucaDegasperiOAuth2ServerStorageFluentStorageServiceProvider::class, LucaDegasperiOAuth2ServerOAuth2ServerServiceProvider::class,] "aliases" =>[ "Authorizer" => LucaDegasperiOAuth2ServerFacadesAuthorizer::class,]
數(shù)據(jù)表生成
php artisan migrate
路由
//Oauth2登錄 Route::post("oauth/access_token", function () { return Response::json(Authorizer::issueAccessToken()); });拓展
或者說接下來還需要完善的一些東西
scss頁面重構(gòu),現(xiàn)在的頁面的確很low;
把前端驗證通過vuejs、vue-form、vue-resource進一步完善;
學(xué)習(xí)vue-router的使用節(jié)省不必要的跳轉(zhuǎn);
文檔、文檔、文檔,積累很重要,還有很多要琢磨的東西,一步步來。
能做的太少,要做要學(xué)的很多,得計劃著來
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/21224.html
摘要:最近自己在做小程序練習(xí),分享一下我遇到的小坑數(shù)據(jù)更新直接對進行賦值,是無法更新視圖綁定的數(shù)據(jù)的,會造成數(shù)據(jù)不一致需要使用更新暫時不支持絕對路徑不能使用靜態(tài)文件,只能使用和網(wǎng)絡(luò)圖片可以用 最近自己在做小程序練習(xí),分享一下我遇到的小坑 data數(shù)據(jù)更新 直接對this.data進行賦值,是無法更新視圖綁定的數(shù)據(jù)的,會造成數(shù)據(jù)不一致 需要使用this.setData更新 this.dat...
摘要:關(guān)于本教程有任何建議或者疑問,都歡迎郵件與我聯(lián)系,或者在上提出教程流程簡介教程將會從如何分析微信協(xié)議開始,第一部分將教你如何從零開始獲取并模擬擴展個人微信號所需要的協(xié)議。 現(xiàn)在的日常生活已經(jīng)離不開微信,難免會生出微信有沒有什么API可以使用的想法。 那樣就可以拿自己微信做個消息聚合、開個投票什么的,可以顯然沒有這種東西。 不過還好,有網(wǎng)頁版微信不就等于有了API么,這個項目就是出于這個...
摘要:提高有了入門的基礎(chǔ),開始自學(xué)當時流行的三大框架和。業(yè)余的時間,經(jīng)常在上閑逛,看一些博客或開源的代碼。 最近有一位小伙伴通過公眾號給我留言, 我參加工作沒多久,看著圈里的技術(shù)大牛,特別羨慕,也渴望成為技術(shù)大牛,想讓您分享一下從小白到大牛是怎樣練成的,我該如何提高自己 首先,謝謝這位小伙伴的一直關(guān)注。其次,我并不是大牛,只是早搬了幾年的磚而已,不過可以分享一下我的Java開發(fā)之路。 入門 ...
摘要:有談?wù)劽嬖嚺c面試題對于前端面試的一些看法。動態(tài)規(guī)劃算法的思想及實現(xiàn)方法幫大家理清動態(tài)規(guī)劃的解決思路以及原理方法前端經(jīng)典面試題從輸入到頁面加載發(fā)生了什么這是一篇開發(fā)的科普類文章,涉及到優(yōu)化等多個方面。極客學(xué)院前端練習(xí)題道練習(xí)題,面試季練練手。 由數(shù)據(jù)綁定和排序引入的幾個 JavaScript 知識點 在 JavaScript 的數(shù)據(jù)綁定和做簡單的表格排序中遇到的幾個知識點 [[JS 基礎(chǔ)...
摘要:第一部分介紹了如何使用和開發(fā)接口。由于系統(tǒng)變得越來越復(fù)雜,人們提出了稱為預(yù)處理器和后處理器的工具來管理復(fù)雜性。當您第一次得知有預(yù)處理器和后處理器時,你很有可能在任何地方已經(jīng)使用它們。我之前建議的文章,,也涵蓋了預(yù)處理器相關(guān)的知識。 我記得我剛開始學(xué)習(xí)前端開發(fā)的時候。我看到了很多文章及資料,被學(xué)習(xí)的資料壓得喘不過氣來,甚至不知道從哪里開始。 本指南列出前端學(xué)習(xí)路線,并提供了平時收藏的一些...
閱讀 2450·2021-10-11 10:57
閱讀 1300·2021-10-09 09:59
閱讀 2014·2019-08-30 15:53
閱讀 3228·2019-08-30 15:53
閱讀 1028·2019-08-30 15:45
閱讀 759·2019-08-30 15:44
閱讀 3470·2019-08-30 14:24
閱讀 968·2019-08-30 14:21