victorbuild / modern-web-21-demo Goto Github PK
View Code? Open in Web Editor NEW簡易文章CRUD
簡易文章CRUD
新增一個 ArticleService.php
檔案,於 app\Services
資料夾內
通常系統設計上,複雜度會比範例還要複雜許多,因此除了MVC 架構以外,通常會在拆分將 商業邏輯
存放於 Service 的類別中。
同上個 issue #7 新建要有新建紀錄,修改也要有修改紀錄(每次不同的修改內容),你會怎麼做? 歡迎提出怎麼製作的方法討論,沒有標準答案。
因為它是額外操作紀錄,所以Victor 會建立 observers 來監聽Model事件,Article Model 的新增、修改或刪除紀錄,新建在另外一張資料表上保存LOG
詳情請見Laravel 文件。
https://laravel.com/docs/8.x/eloquent#observers
ArticleService
使用依賴注入的方式 呼叫 ArticleRespository
的查詢資料庫邏輯的方法
Controller 控制流程 Service 拆分的各個商業邏輯,資料庫相關的操作邏輯的詳細內容,由ArticleRespository
負責,Victor 習慣統一由 service 的檔案中呼叫ArticleRespository
,眾多的商業邏輯中,關於操作資料庫的部分,呼叫ArticleRespository
來執行。
ArticleController
建立一個私有變數 $articleRespositoryArticleRespository
中你想要呼叫的方法。依照商業邏輯區分多個方法,讓每個方法做一個商業的邏輯。
Controller
通常就是控制流程,依照目的性將商業邏輯,分別拆分多個方法,新增在前一個小練習 ArticleService
檔案中,此檔案目的為專門處理 Article
資源商業邏輯所有方法。
這裡沒有真的要撰寫商業邏輯,替商業邏輯方法命名,並使用註解的方法,簡易撰寫商業邏輯的詳細流程即可,如下範例,建立一個 sendEmail
方法於ArticleService
用於表示新建文章成功的通知信,還有很多可以做的事情,沒有標準答案主要就是每個方法做一件商業邏輯,新建於ArticleService
的練習。
public function sendEmail(User $user)
{
// TODO 取得會員EMAIL
// TODO 沒有EMAIL 處理辦法
// TODO 有EMAIL 寄送EMAIL
// 回傳 寄出、未寄出的布林值
}
假設我們是都是同一間公司的同事,假設今天收到了,修改系統需要在,新增文章要留下新建紀錄的Log在另外一張資料表。
於下方回覆是在哪一個檔案的哪一個方法修改修改這個功能即可。
這也是範例Controller 使用 Laravel CRUD 預設方法名稱的原因,達到共識,共同維護時會比較方便,知道應該在哪裡修改程式碼,縮短探索程式碼時間。
Controller 控制流程,其他詳細的商業邏輯於 Article Service 類別中撰寫,此小練習將在Controller 呼叫 service 的方法。
依賴注入減少耦合,在建立實體時,一並帶入實體。
ArticleController
建立一個私有變數 $articleServiceArticleService
中你想要呼叫的方法。A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.