pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

  • <small id='1BdY8'></small><noframes id='1BdY8'>

  • <i id='1BdY8'><tr id='1BdY8'><dt id='1BdY8'><q id='1BdY8'><span id='1BdY8'><b id='1BdY8'><form id='1BdY8'><ins id='1BdY8'></ins><ul id='1BdY8'></ul><sub id='1BdY8'></sub></form><legend id='1BdY8'></legend><bdo id='1BdY8'><pre id='1BdY8'><center id='1BdY8'></center></pre></bdo></b><th id='1BdY8'></th></span></q></dt></tr></i><div class="msfav8h" id='1BdY8'><tfoot id='1BdY8'></tfoot><dl id='1BdY8'><fieldset id='1BdY8'></fieldset></dl></div>

        • <bdo id='1BdY8'></bdo><ul id='1BdY8'></ul>
        <tfoot id='1BdY8'></tfoot>
        <legend id='1BdY8'><style id='1BdY8'><dir id='1BdY8'><q id='1BdY8'></q></dir></style></legend>

        從“胖模型,瘦控制器"工作的做法是什么?對

        What are the practices for working from a quot;Fat model, skinny controllerquot; perspective with Laravel Eloquent ORM?(從“胖模型,瘦控制器工作的做法是什么?對 Laravel Eloquent ORM 的看法?) - IT屋-程序員軟件開發(fā)技

          <i id='oOibm'><tr id='oOibm'><dt id='oOibm'><q id='oOibm'><span id='oOibm'><b id='oOibm'><form id='oOibm'><ins id='oOibm'></ins><ul id='oOibm'></ul><sub id='oOibm'></sub></form><legend id='oOibm'></legend><bdo id='oOibm'><pre id='oOibm'><center id='oOibm'></center></pre></bdo></b><th id='oOibm'></th></span></q></dt></tr></i><div class="qddam3l" id='oOibm'><tfoot id='oOibm'></tfoot><dl id='oOibm'><fieldset id='oOibm'></fieldset></dl></div>
          <legend id='oOibm'><style id='oOibm'><dir id='oOibm'><q id='oOibm'></q></dir></style></legend>
              <tbody id='oOibm'></tbody>

                <small id='oOibm'></small><noframes id='oOibm'>

                  <bdo id='oOibm'></bdo><ul id='oOibm'></ul>

                • <tfoot id='oOibm'></tfoot>
                • 本文介紹了從“胖模型,瘦控制器"工作的做法是什么?對 Laravel Eloquent ORM 的看法?的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  閱讀后,我一直在為胖模型,瘦控制器"的概念挑選其他開發(fā)人員的大腦:

                  I've been picking other developers' brains on the concept of "fat models, skinny controllers" after reading:

                  • http://culttt.com/2013/07/01/setting-up-your-first-laravel-4-controller/
                  • http://culttt.com/2013/05/13/setting-up-your-first-laravel-4-model/

                  大多數(shù)受訪者正在使用我認(rèn)為的胖控制器.

                  Most respondents are using what I'd consider fat controllers.

                  雖然這個(gè)話題出現(xiàn)在 Stack Overflow 上,但我還沒有在實(shí)踐中找到對該方法的詳盡描述.

                  While the topic has come up on Stack Overflow I haven't found thorough description of the method in practice.

                  我剛剛在這里找到了一個(gè)舊的相關(guān)問題.

                  I just found an old related question here.

                  推薦答案

                  Skinny Controllers

                  您將在 PHP(vanilla 或 Laravel 或 Symfony)中看到的越來越多的是有史以來最瘦的控制器.這是您在 Rails 中已經(jīng)看到的東西,人們也開始將其稱為(通過其他一些實(shí)踐)六邊形.你的控制器中只需要一行代碼,實(shí)際上他們說這應(yīng)該是你所有方法的目標(biāo).這是一個(gè)例子,是的,比這多一點(diǎn),但仍然很瘦:

                  Skinny Controllers

                  What you are going to see in PHP (vanilla or Laravel or Symfony) more and more are the skinniest controllers ever. This is something you already see in Rails, and people are also starting to call it (with some other practices) hexagonal. One line of code is all you need in your controller, actually they say this should be a goal for all your methods. This is an example with, yes, a little bit more than that, but still skinny:

                  <?php
                  
                  class PostController extends Controller {
                  
                      private $repository;
                  
                      public function __construct(PostRepositoryInterface $repository)
                      {
                          $this->repository = $repository;
                      }
                  
                      public function store()
                      {
                          try 
                          {
                              $this->repository->create(Input::all());
                          }
                          catch (ValidationException $e) 
                          {
                              return Redirect::back()->withInput()->withErrors($e->all());
                          }
                  
                          return Redirect::route('posts');
                      }
                  
                  }
                  

                  控制器是 HTTP 請求、業(yè)務(wù)邏輯和表示層之間的橋梁.所以它應(yīng)該接收一個(gè)請求,將它發(fā)送到一個(gè)注入的對象,該對象將處理它并重定向到負(fù)責(zé)向客戶端(或用戶)提供反饋的路由(或呈現(xiàn)視圖).其他一切,包括驗(yàn)證,都應(yīng)該在您的存儲(chǔ)庫、服務(wù)、模型(MVC,是的!)等中進(jìn)行.

                  A controller is a bridge between the HTTP requests, your business logic and your presentation layer. So it should receive one request, send it to an injected object which will process it and redirect to the route (or render a view) responsible for giving feedback to a client (or user). Everything else, including validation, should happen in your repositories, services, models (MVC, yay!), etc.

                  但是我們可以以六邊形的方式重構(gòu)這個(gè)控制器,以達(dá)到每個(gè)方法一行的目標(biāo):

                  But we could refactor this controller, in the hexagonal way, to reach the one-line-per-method goal:

                  <?php
                  
                  class PostController extends Controller {
                  
                      private $repository;
                  
                      public function __construct(PostRepositoryInterface $repository)
                      {
                          $this->repository = $repository;
                      }
                  
                      public function store()
                      {
                          return $this->repository->create(Input::all(), $this);
                      }
                  
                      public function createSucceeded()
                      {
                          return Redirect::route('posts');
                      }
                  
                      public function createFailed()
                      {
                          return Redirect::back()->withInput()->withErrors($e->all());
                      }
                  
                  }
                  

                  基本上,您的存儲(chǔ)庫類將使用自己的調(diào)用者 ($this) 來觸發(fā) succeededfailed 方法.

                  Basically your repository classes will use the own caller ($this) to fire the succeeded and failed methods.

                  模型與您的數(shù)據(jù)過于相關(guān),有時(shí)它們是您的 ORM 并直接與您的數(shù)據(jù)庫服務(wù)器通信,因此,現(xiàn)在您會(huì)看到人們使用存儲(chǔ)庫和服務(wù)作為它們之間的層.

                  Models are too related to your data, sometimes they are your ORM and talk directly to your database server, so, these days you'll see people use repositories and services as layers between them.

                  存儲(chǔ)庫是一個(gè)類,它通過直接與您的模型對話來處理和收集您的應(yīng)用程序所需的信息.您的應(yīng)用程序不應(yīng)該知道在您的數(shù)據(jù)庫中選擇某些信息需要什么,選擇,位置,排序,分組依據(jù),這些有時(shí)只有您的模型應(yīng)該知道,所以這是一個(gè)存儲(chǔ)庫:

                  A repository is a class that, by talking directly to your models, processes and gather the information your application needs. Your application should not be aware of what is necessary to select some information in your database, select, where, order, group by, those are things sometimes only your models should be aware of, so this is a repository:

                  class PostRepository implements PostRepositoryInterface {
                  
                      private $model;
                  
                      public function __construct(PostInterface $model)
                      {
                          $this->model = $model;
                      }
                  
                      public function create($input)
                      {
                          return $this->model->create($input);
                      }
                  
                      public findBySlug($slug)
                      {
                          return $this->model->where('slug', $slug)->first();
                      }
                  
                  }
                  

                  服務(wù)

                  所有不直接屬于您的業(yè)務(wù)邏輯的東西,主要是外部服務(wù),離您的應(yīng)用程序代碼最遠(yuǎn)的地方,您構(gòu)建的越解耦越好.為這些服務(wù)創(chuàng)建外部包(Composer 包)是將它們與其他一切分離的好方法,如果你讓它們與框架無關(guān),你就有權(quán)獲得 10 點(diǎn)鱘魚點(diǎn).在 Laravel 中,您可以通過集成三種類來創(chuàng)建服務(wù):

                  Services

                  Everything that doesn't belongs directly to your business logic, mostly external services, the farthest from your application code, the more decoupled you build them, the better. Creating external packages (Composer packages) for those services are a good way of decoupling them from everything else, and you if you make them framework agnostic you're entitled to receive 10 Sturgeon points. In Laravel you can create services by integrating three kind of classes:

                  1) Service Class(es):負(fù)責(zé)做你的服務(wù)必須做的事情,你所有的服務(wù)邏輯都在這里.

                  1) Service Class(es): responsible for doing what your service must do, all your service logic goes here.

                  2) 服務(wù)提供者:負(fù)責(zé)啟動(dòng)你的服務(wù)并將其添加到 Laravel 的 IoC 容器中,以便隨時(shí)可以使用,但請注意,Laravel 只會(huì)在你的應(yīng)用程序真正使用它們時(shí)實(shí)例化你的服務(wù)類.

                  2) Service Provider: responsible for booting up your service and adding it to Laravel's IoC container so it can be ready to use at any time, but note that Laravel will only instantiate your service classes when your application really use them.

                  3) Facade:允許您使用靜態(tài) (::) 語法從應(yīng)用程序的任何位置訪問您的服務(wù):

                  3) Facade: lets you access your service from anywhere in your application using the static (::) syntax:

                  Mailer::send($user->id, 'Thanks for registering', 'emails.registered');
                  

                  這是郵件程序服務(wù):

                  <?php namespace ACRServicesMailer;
                  
                  use IlluminateMailMailer as IlluminateMailer;
                  use Sentry;
                  
                  class Service {
                  
                      public function __construct(IlluminateMailer $mailer)
                      {
                          $this->mailer = $mailer;    
                      }
                  
                      public function send($userId, $subject, $view, $data = [])
                      {
                          return $this->mailer->queue($view, $data, function($message) use ($userId, $subject)
                          {
                              $user = Sentry::findUserById($userId);
                  
                              $message->to($user->email, $user->name);
                  
                              $message->subject($subject);
                          });
                      }
                  
                  }
                  

                  服務(wù)提供商

                  <?php namespace ACRServicesMailer;
                  
                  use IlluminateSupportServiceProvider as  IlluminateServiceProvider;
                  use ACRServicesMailerService as Mailer;
                  
                  class ServiceProvider extends IlluminateServiceProvider {
                  
                      /**
                       * Indicates if loading of the provider is deferred.
                       *
                       * @var bool
                       */
                      protected $defer = true;
                  
                      /**
                       * Register the service provider.
                       *
                       * @return void
                       */
                      public function register()
                      {
                          $this->app->bind('acr.mailer', function($app) {
                  
                              return new Mailer($app->make('mailer'));
                  
                          });
                      }
                  
                      /**
                       * Get the services provided by the provider.
                       *
                       * @return array
                       */
                      public function provides()
                      {
                          return array('acr.mailer');
                      }
                  
                  }
                  

                  立面

                  <?php namespace ACRServicesMailer;
                  
                  use IlluminateSupportFacadesFacade as IlluminateFacade;
                  
                  class Facade extends IlluminateFacade {
                  
                      protected static function getFacadeAccessor() { return 'acr.mailer'; }
                  
                  }
                  

                  模型/ORM

                  那些人應(yīng)該是高度可交換的,今天您可能使用 Eloquent 作為 ORM,將數(shù)據(jù)存儲(chǔ)在數(shù)據(jù)庫中,但您可能需要將其更改為其他內(nèi)容,有些人將 100% 的數(shù)據(jù)存儲(chǔ)在 Redis 中,因此,您最好通過在 ORM 和域 loginc 之間使用接口(合同)層來為這樣的更改做好準(zhǔn)備,并真正為您的接口而不是您的具體類進(jìn)行開發(fā).Taylor Otwell 在他的書中甚至說你應(yīng)該完全刪除你的模型文件夾.

                  Models / ORM

                  Those guys should be highly swappable, today you may be using a Eloquent as your ORM, storing data in a database, but you might need to change it to something else, some foks are storing 100% of their data in Redis, so you better be prepared for a change like this by using an Interface (contract) layer between your ORM and your domain loginc and really develop for your interfaces, not your concrete classes. Taylor Otwell in his book even say that you should completely delete your models folder.

                  interface PostInterface {
                  
                      public function all();
                  
                      public function find($id);
                  
                  }
                  
                  class DbPost extends Eloquent implements PostInterface {
                  
                  }
                  
                  class RedisPost extends Eloquent implements PostInterface {
                  
                  }
                  

                  這背后的想法是很容易交換實(shí)現(xiàn),所以在 Laravel 中你可以使用 IoC 容器來告訴 Laravel 你正在使用哪個(gè)實(shí)現(xiàn):

                  The idea behind this is to swap implementations easily, so in Laravel you can use the IoC container to tell Laravel which implementation you are using:

                  App::bind('PostInterface', 'DbPost');
                  

                  因此,如果您有一個(gè)存儲(chǔ)庫正在使用您的 PostInterface:

                  So, if you have a Repository is using your PostInterface:

                  class PostRepository implements PostRepositoryInterface {
                  
                      private $model;
                  
                      public function __construct(PostInterface $model)
                      {
                          $this->model = $model;
                      }
                  
                  }
                  

                  Laravel IoC 容器將使用 DbPost 實(shí)例自動(dòng)實(shí)例化此存儲(chǔ)庫.如果您需要將其更改為 Redis,則只需更改一行:

                  Laravel IoC container will automatically instantiate this repository with an instance of DbPost. And if you ever need to change it to Redis, you just need to change one line:

                  App::bind('PostInterface', 'RedisPost');
                  

                  觀看次數(shù)/演示者

                  最笨的就是最棒的.

                  Views / Presenters

                  The dumbest the awesomer.

                  視圖應(yīng)該只負(fù)責(zé)顯示信息.視圖不應(yīng)該知道您的模型、服務(wù)、存儲(chǔ)庫或系統(tǒng)中的任何其他內(nèi)容.視圖應(yīng)該可以由 websigners 編輯,因此,您擁有的代碼越多,您的非 php-programmer-designer 向它們添加的錯(cuò)誤就越多.您的控制器應(yīng)該從您的存儲(chǔ)庫中收集信息并將它們傳遞給您的視圖:

                  Views should be responsible only for displaying information. Views should not be aware of your models, services, repositories, or anything else in your system. Views should be editable by webesigners so, the more code you have on them, the more bugs your non-php-programmer-designer will add to them. Your controller should gather the information from your repositories and pass them to your views:

                  <?php
                  
                  class PostController extends Controller {
                  
                      private $repository;
                  
                      public function __construct(PostRepositoryInterface $repository)
                      {
                          $this->repository = $repository;
                      }
                  
                      public function index()
                      {
                          return View::make('posts.index')->with('posts', $this->repository->getPaginated());
                      }
                  
                  }
                  

                  您的視圖的唯一責(zé)任應(yīng)該是顯示數(shù)據(jù):

                  And the only responsibility of your view should be show that data:

                  @extends('layout')
                  
                  @section('contents')
                      <ul>
                          @foreach($posts as $post)
                              <li>
                                  {{ $post->title }} - {{ $post->author }} - {{ $post->published_at }}
                              </li>
                          @endforeach
                      </ul>
                  
                      {{ $users->links() }}
                  @stop
                  

                  演示者

                  您如何格式化數(shù)據(jù)?您在視圖中編寫原始屬性,但您應(yīng)該在幕后使用演示者來展示您的數(shù)據(jù).展示者通常使用裝飾器設(shè)計(jì)模式來格式化要在頁面中展示的數(shù)據(jù).這是一個(gè)使用 Shawn McCool 的 LaravelAutoPresenter 的例子:

                  Presenters

                  How do you format your data? You write raw properties in your views, but you should, behind the scenes, be using presenters to, yeah, present your data. Presenters usually use the Decorator Design Pattern to format your data to be presented in your pages. This is an example using Shawn McCool's LaravelAutoPresenter:

                  <?php namespace AppPresenters;
                  
                  use McCoolLaravelAutoPresenterBasePresenter;
                  
                  class Post extends BasePresenter {
                  
                      public function __construct(UserModel $user)
                      {
                          $this->resource = $user;
                      }
                  
                      public function author()
                      {
                          return $this->resource->author->name;
                      }
                  
                      public function published_at()
                      {
                          return $this->date($this->resource->created_at);
                      }
                  
                      public function dateTime($date)
                      {
                          return CarbonCarbon::createFromFormat('d-m-Y', $date, 'Sao_Paulo/Brazil')
                                       ->toFormattedDateString();
                      }    
                  }
                  

                  相關(guān)書籍

                  Taylor Otwell 的 Laravel:從學(xué)徒到工匠

                  Chris Fidao 的 Laravel 實(shí)現(xiàn)

                  這篇關(guān)于從“胖模型,瘦控制器"工作的做法是什么?對 Laravel Eloquent ORM 的看法?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  MySQLi prepared statement amp; foreach loop(MySQLi準(zhǔn)備好的語句amp;foreach 循環(huán))
                  Is mysqli_insert_id() gets record from whole server or from same user?(mysqli_insert_id() 是從整個(gè)服務(wù)器還是從同一用戶獲取記錄?)
                  PHP MySQLi doesn#39;t recognize login info(PHP MySQLi 無法識(shí)別登錄信息)
                  mysqli_select_db() expects exactly 2 parameters(mysqli_select_db() 需要 2 個(gè)參數(shù))
                  Php mysql pdo query: fill up variable with query result(Php mysql pdo 查詢:用查詢結(jié)果填充變量)
                  MySQLI 28000/1045 Access denied for user #39;root#39;@#39;localhost#39;(MySQLI 28000/1045 用戶“root@“l(fā)ocalhost的訪問被拒絕)
                        <tbody id='fs1lG'></tbody>

                      <i id='fs1lG'><tr id='fs1lG'><dt id='fs1lG'><q id='fs1lG'><span id='fs1lG'><b id='fs1lG'><form id='fs1lG'><ins id='fs1lG'></ins><ul id='fs1lG'></ul><sub id='fs1lG'></sub></form><legend id='fs1lG'></legend><bdo id='fs1lG'><pre id='fs1lG'><center id='fs1lG'></center></pre></bdo></b><th id='fs1lG'></th></span></q></dt></tr></i><div class="3nzya3y" id='fs1lG'><tfoot id='fs1lG'></tfoot><dl id='fs1lG'><fieldset id='fs1lG'></fieldset></dl></div>
                      • <small id='fs1lG'></small><noframes id='fs1lG'>

                          <legend id='fs1lG'><style id='fs1lG'><dir id='fs1lG'><q id='fs1lG'></q></dir></style></legend>
                            <bdo id='fs1lG'></bdo><ul id='fs1lG'></ul>
                            <tfoot id='fs1lG'></tfoot>

                            主站蜘蛛池模板: CCC验厂-家用电器|服务器CCC认证咨询-奥测世纪 | 论文查重_免费论文查重_知网学术不端论文查重检测系统入口_论文查重软件 | 阳光模拟试验箱_高低温试验箱_高低温冲击试验箱_快速温变试验箱|东莞市赛思检测设备有限公司 | 洁净实验室工程-成都手术室净化-无尘车间装修-四川华锐净化公司-洁净室专业厂家 | 航空连接器,航空插头,航空插座,航空接插件,航插_深圳鸿万科 | 天津货架厂_穿梭车货架_重型仓储货架_阁楼货架定制-天津钢力仓储货架生产厂家_天津钢力智能仓储装备 | 臭氧老化试验箱,高低温试验箱,恒温恒湿试验箱,防水试验设备-苏州亚诺天下仪器有限公司 | 杭州门窗厂家_阳光房_包阳台安装电话-杭州窗猫铝合金门窗 | DAIKIN电磁阀-意大利ATOS电磁阀-上海乾拓贸易有限公司 | 潍坊青州古城旅游景点攻略_青州酒店美食推荐-青州旅游网 | 电伴热系统施工_仪表电伴热保温箱厂家_沃安电伴热管缆工业技术(济南)有限公司 | 保温杯,儿童婴童奶瓶,运动水壶「广告礼品杯定制厂家」超朗保温杯壶 | 石家庄网站建设|石家庄网站制作|石家庄小程序开发|石家庄微信开发|网站建设公司|网站制作公司|微信小程序开发|手机APP开发|软件开发 | 不发火防静电金属骨料_无机磨石_水泥自流平_修补砂浆厂家「圣威特」 | 沈阳激光机-沈阳喷码机-沈阳光纤激光打标机-沈阳co2激光打标机 | 超声波成孔成槽质量检测仪-压浆机-桥梁预应力智能张拉设备-上海硕冠检测设备有限公司 | 炭黑吸油计_测试仪,单颗粒子硬度仪_ASTM标准炭黑自销-上海贺纳斯仪器仪表有限公司(HITEC中国办事处) | 杜甫仪器官网|实验室平行反应器|升降水浴锅|台式低温循环泵 | 脱硝喷枪-氨水喷枪-尿素喷枪-河北思凯淋环保科技有限公司 | 湖南长沙商标注册专利申请,长沙公司注册代理记账首选美创! | 有福网(yofus.com)洗照片冲印,毕业聚会纪念册相册制作个性DIY平台 | 金属回收_废铜废铁回收_边角料回收_废不锈钢回收_废旧电缆线回收-广东益夫金属回收公司 | 深圳美安可自动化设备有限公司,喷码机,定制喷码机,二维码喷码机,深圳喷码机,纸箱喷码机,东莞喷码机 UV喷码机,日期喷码机,鸡蛋喷码机,管芯喷码机,管内壁喷码机,喷码机厂家 | 断桥铝破碎机_铝合金破碎机_废铁金属破碎机-河南鑫世昌机械制造有限公司 | 成都租车_成都租车公司_成都租车网_众行宝 | 视频教程导航网_视频教程之家_视频教程大全_最新视频教程分享发布平台 | 电磁铁_推拉电磁铁_机械手电磁吸盘电磁铁厂家-广州思德隆电子公司 | 护栏打桩机-打桩机厂家-恒新重工 | 耐高温风管_耐高温软管_食品级软管_吸尘管_钢丝软管_卫生级软管_塑料波纹管-东莞市鑫翔宇软管有限公司 | 杭州顺源过滤机械有限公司官网-压滤机_板框压滤机_厢式隔膜压滤机厂家 | 锌合金压铸-铝合金压铸厂-压铸模具-冷挤压-誉格精密压铸 | 蒜肠网-动漫,二次元,COSPLAY,漫展以及收藏型模型,手办,玩具的新媒体.(原变形金刚变迷TF圈) | 青州开防盗门锁-配汽车芯片钥匙-保险箱钥匙-吉祥修锁店 | 温控器生产厂家-提供温度开关/热保护器定制与批发-惠州市华恺威电子科技有限公司 | 沈阳液压泵_沈阳液压阀_沈阳液压站-沈阳海德太科液压设备有限公司 | 四川成都干燥设备_回转筒干燥机_脉冲除尘器_输送设备_热风炉_成都川工星科机电设备有限公司 | 重庆钣金加工厂家首页-专业定做监控电视墙_操作台 | 云阳人才网_云阳招聘网_云阳人才市场_云阳人事人才网_云阳人家招聘网_云阳最新招聘信息 | 蓝米云-专注于高性价比香港/美国VPS云服务器及海外公益型免费虚拟主机 | 逗网红-抖音网红-快手网红-各大平台网红物品导航 | 磁棒电感生产厂家-电感器厂家-电感定制-贴片功率电感供应商-棒形电感生产厂家-苏州谷景电子有限公司 |