久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      如何寫出高質(zhì)量的PHP代碼

      如何寫出高質(zhì)量的PHP代碼

      寫給初生牛犢不怕虎的童鞋們,大佬可隨意摘看 本章基于PHP Laravel

      前言

      經(jīng)常會有人問

      • 目錄如何設(shè)計比較好?
      • 代碼如何分布好?
      • 怎么寫一個可維護的項目?

      “爛”項目我也沒少寫,以下是參考互聯(lián)網(wǎng)各大佬的文章總結(jié)及個人開發(fā)經(jīng)驗而來.

      Controller

      如何寫出高質(zhì)量的PHP代碼

      Controller顧名思義是控制器,在入門PHP的時候,就知道Controller代表MVC中的C層,MVC本身的概念就代碼分離,教你如何如何將業(yè)務(wù)分開,但面臨著業(yè)務(wù)的不斷發(fā)展,代碼的復雜度也隨之提高,功能與功能之間的鏈接錯綜復雜,最后你的MVC就變成了下圖,所以僅僅依托MVC的設(shè)計思想已經(jīng)無法支撐不斷發(fā)展的業(yè)務(wù)。

      現(xiàn)在我們將Controller的任務(wù)和能力重新定義,控制器僅僅控制Http Reqeust的請求,這樣就符合了SOLID 單一功能原則.

      如何寫出高質(zhì)量的PHP代碼

      直接將業(yè)務(wù)代碼寫在Controller中,會使得代碼及其臃腫,不易于維護和擴展

      <?php 	namespace AppHttpController;  	class UserController extends Controller{  		public function register(Request $request){			$user = new User();			$user->username = $request->input('username');			$user->password = $request->input('password');			$result = $user->save();			return $result; 		}  	}復制代碼

      這時就應(yīng)該思考如何分離業(yè)務(wù)代碼,我們引入Service的概念

      Service

      Service本身譯為服務(wù)

      • 將外部方法,公共方法注入到Service
      • 將Service注入到控制器
      如何寫出高質(zhì)量的PHP代碼

      像上圖這樣

      UserController

      <?php 	namespace AppHttpController;  	class UserController extends Controller{  		public $request; 		 		protected $userService; 		 		public function __construct(Request $request, UserService $userService) 		{			$this->request = $request;			 			$this->userService = $userService; 		} 		 		public function register() 		{ 			//... validation			return $this->userService->register ($this->request->all()); 		}  	}復制代碼

      UserService

      <?php 	namespace AppService;      class UserService{              public function register($data) 		{            $username = $data['username'];            $password = $data['password'];          			$password = encrypt ($password);			 			$user = new User();			$user->username = $username;			$user->password = $password;			$result = $user->save();			return $result; 		}      }復制代碼

      到現(xiàn)在為止,我們至少將業(yè)務(wù)與請求徹底分開了。但還是不如人意,如果把所有的業(yè)務(wù)及CURD全部寫在Service中,那只不過是將Controller的臃腫轉(zhuǎn)移到了Service,那Service就沒有什么存在意義了。 所以我們需要繼續(xù)分割Service,將對數(shù)據(jù)庫的R操作獨立出來,因為CUD的操作基本是一貫不變的,而R操作根據(jù)業(yè)務(wù)的復雜度則變的多姿多彩。所以獨立R操作。這個時候我們引用Repository的概念。

      Repository

      我們使用Repository輔助Model,將相關(guān)的查詢邏輯封裝到不同的repository中,方便邏輯代碼的維護

      • 符合SOLID的單一原則
      • 符合SOLID的依賴反轉(zhuǎn)
      如何寫出高質(zhì)量的PHP代碼

      UserController

      <?php 	namespace AppHttpController;  	class UserController extends Controller{  		public $request; 		 		protected $userService; 		 		public function __construct(Request $request, UserService $userService) 		{			$this->request = $request;			 			$this->userService = $userService; 		} 		 		public function getUserInfo() 		{ 			//... validation			return $this->userService->getUserInfo ($this->request->all()); 		}  	}復制代碼

      UserService

      <?php 	namespace AppService;      class UserService{         public $userRepository;                  public function __construct(UserRepository $userRepository){            $this->userRepository = $userRepository;         }         public function getUserInfo() 		{            return $this->userRepository->getUserInfo($data); 		}      }復制代碼

      UserRepository

      <?php 	namespace AppRepository;      class UserRepository{              public function getUserInfo($data) 		{            $userId = $data['user_id'];            $result = User::where('id',$userId)->first();			 			return $result; 		}      }復制代碼

      解決了R的問題,有人就問了,難道因為CUD比較統(tǒng)一簡單就可以放在一起了嗎?答案是NO,我們引用一個新的名詞Action。

      Action

      這是看了@Charlie_Jade的文章才學到的

      獨立每個操作文件,例如CreateUser,DeleteUser,UpdateUser

      • 符合SOLID的單一原則
      如何寫出高質(zhì)量的PHP代碼

      UserController

      <?php 	namespace AppHttpController;  	class UserController extends Controller{  		public $request; 		 		protected $userService; 		 		public function __construct(Request $request, UserService $userService) 		{			$this->request = $request;			 			$this->userService = $userService; 		} 		         public function register(){             //... validation            return $this->userService->register($this->request->all());         }  		public function getUserInfo() 		{			return $this->userService->getUserInfo ($this->request->all()); 		}  	}復制代碼

      UserService

      <?php 	namespace AppService;      class UserService{                  public function getUserInfo(UserRepository $userRepository) 		{            return $this->userRepository->getUserInfo($data); 		}          public function register(){            $result = (new CreateUser())->execute($this->request->all());                         return $result;         }      }復制代碼

      UserRepository

      <?php 	namespace AppRepository;      class UserRepository{              public function getUserInfo($data) 		{            $userId = $data['user_id'];            $result = User::where('id',$userId)->first();			 			return $result; 		}      }復制代碼

      CreateUser

      <?php  	namespace AppAction; 	 	use AppModelMember; 	 	class CreateUser extends CreateUserWallet 	{ 		public function execute(array $data) 		{			$models           = new Member();			$models->tel      = $data['tel'];			$models->password = $data['password'];			$result           = $models->save ();				 			return $result; 		} 	}復制代碼

      以上代碼邏輯見下圖

      如何寫出高質(zhì)量的PHP代碼

      除模版(V)等HTML,JS等,還需要一些其他的規(guī)則,或者說是方式去實現(xiàn)一些代碼的解耦合,以下不再提供代碼案例。

      Common

      譯為公共的,常用的,再部分開發(fā)中,你可能需要一些公共的方法(并非公共的類,例如郵件發(fā)送等,用他并不合適),比如查詢用戶余額,查詢用戶是否注冊或者是否在線,生成訂單號等。使用Common更要簡單。他更像一個公共函數(shù)庫的樣子

      如何寫出高質(zhì)量的PHP代碼

      Event

      不關(guān)心執(zhí)行結(jié)果時可以選使用,不過Event的Listen也是提供了隊列。

      Exception

      不要將你的所有錯誤提示都使用Return返回,很多時候你的返回未必是你的返回

      致謝

      感謝各位同學看完這篇文章,如果你有新的想法歡迎在評論區(qū)討論.

      推薦教程:《php教程》

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號