dtvfxzq3802 2014-10-20 01:51
浏览 33
已采纳

Laravel应用程序结构:我应该何时使用模型,何时应该使用帮助程序类?

I've been learning MVC for the past week, specifically, the Laravel PHP framework. I've signed up for Laracasts, and I have a basic understanding of how the MVC pattern works, but one thing which has not been explained to me is just when to use a Model.

Every time I've been demonstrated what a model is, it has extended Eloquent and linked to a database table. Jeffrey Way has specifically noted not to conflate "model" with "table", so I'm assuming you can create and use models that aren't linked in to a database table, but I'm unsure of when to do so.

Moreover, I'm not entirely certain about this: When should I use a model, and when should I use a helper class/library? The Laravel documentation does not cover models in much detail, and only talks about Eloquent mainly.

I can think of two examples where I'm uncertain of where my code should be placed:

A navigation creator

Say I have a set of articles delivered by an Article extends Eloquent model, linked in to a articles database table. What happens if I want to create a set of dynamic navigation links to other pages, such as the previous article, the next article, an article nearby, and a similar article.

This would serve a pretty large and crucial component of a site. I could imagine each of the four previous code-highlighted examples being a method of a class, but should that class itself be an independent Model, or merely a helper class of Article?

An image builder

Say each article has images embedded inside them in some easy-to-write form. I could write an ImageBuilder class which could parse each article and replace my embedded user-friendly image syntax with complex HTML based on the type of image and what position it should have in the document.

This seems highly dependent on the Article class itself, so I'd be leaning towards it being a helper class rather than a model in itself, but I'm still not sure.


Where is the distinction? Where do I draw the line? Specifically with my examples, what would you recommend?

  • 写回答

1条回答 默认 最新

  • duanjun7801 2014-10-20 02:12
    关注

    I am not the end all on this topic but will give you my two cents. I am sure you will receive some other great advice from others as well who are much better than I am. I am going to try to keep this simple but this is not exhaustive by any means.

    When should I use a model, and when should I use a helper class/library? The answer you don't want to hear is it depends on your application. For brevity think of models like a blueprint for the items in your application. These are things like Posts, Pages, Articles, Images, Users, etc. All the things that make up your application, and each one has rules on how they relate to each other and are created. If you see this item being reused again and again it may be a model!


    Now on to your specific examples:

    A navigation creator

    To me, previous, next, nearby and similar are all custom query scopes that will be in your Article model. These methods will help your search in a specific way for that model. I would brush up on query scopes because they provide lots of power and flexibility with your models. Something like this could help you in your Article model.

    public function scopePrevious($query)
    {
        $currentArticleId  = $this->id;
        $previousArticleId = $this->id - 1;
    
        return $query->where('id', '=', $previousArticleId);
    } 
    

    This could then be used when searching for the previous article like this:

    $article->previous();
    

    This could was not tested but it is just an example.

    An image builder

    This depends on if you are using a WYSIWYG editor or how you are attaching the images. I am a big fan of creating a separate model for Images and then using a polymorphic relationship so I can use them throughout my whole application and have them interact with any other model that requires an image.

    You may look into Polymorphic relationships for things like Comments, Images, and Documents that may be attached to multiple parts of your application.

    If you are using a WYSIWYG and you have to do a bunch of parsing and there really isn't any database entries for the images, then a helper class loaded in a separate folder may be best for this.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测