【问题标题】:What is the proper way to use the Model in Laravel?在 Laravel 中使用模型的正确方法是什么?
【发布时间】:2015-05-22 02:36:47
【问题描述】:

你能帮我解决这个问题吗?我目前正在自己​​学习 Laravel,并且按照 Laracasts 中的教程进行操作,这很棒。在使用 Laravel 之前,我在我的项目中使用 CodeIgniter 和 Opencart,我开始学习 Laravel 是因为我想学习一个新的框架。

在 CI 和 Opencart 中,您的所有数据库查询都在模型中。但在 Laravel 中,您可以在 Controller 中执行和查询?这是在 Laravel 中查询的正确方法吗?

我在Controller中有这样的代码:

<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Article;
use Illuminate\Http\Request;

class ArticlesController extends Controller {

    public function index() {

        $articles = Article::all();

        return view('articles.index')->with('articles', $articles);

    }

}

【问题讨论】:

标签: php laravel opencart


【解决方案1】:

是的,这非常适合小型应用程序。 但是,对于大型应用程序,我建议使用存储库,因为它们将您的模型与控制器分离 - 这使它们更具可读性和可测试性。

您的 ArticlesController 将转换为如下内容:

<?php namespace App\Http\Controllers;

use App\Repositories\Articles\ArticleRepositoryInterface;

class ArticlesController extends Controller {

    private $articles;

    public function __construct(ArticleRepositoryInterface $articles)
    {
        $this->articles = $articles;
    }

    public function index()
    {
        return view('articles.index')
            ->with('articles', $this->articles->all());
    }

}

查看 Laravels Service Container 以了解 ArticleRepositoryInterface 的自动解析。 Laracasts 在存储库中有一些不错的视频。

【讨论】:

    【解决方案2】:

    存储库对您来说是一个明智的决定。但是为什么呢?
    基本上,存储库是您的应用程序和存储之间的“网关”。
    使用存储库,您可以在一个地方找到您的“数据库查询”。

    让我们考虑一下模型文章。
    无需在需要使用它的所有时间都使用 Articles 的静态实例(Articles::find()Articles::all() 等),只需创建一个 Articles 存储库即可。
    在你的控制器中注入这个 repo(例如),并使用存储在你的 ArticleRepository 中的“功能”。

    什么意思?
    让我们考虑一个文章存储库。我会在我的文章模型应用程序中多次使用什么?我需要全选、按 ID 选择、插入、更新、删除。基本上这些“东西”。那么,如果我把所有这些东西都放在一个地方呢?

    class ArticleRepository {
    
        public function all(){}
        public function getById($id){}
        public function insert($data){}
        public function update($data){}
        public function delete($id){}
    
    }
    

    在你的控制器中注入这个 ArticleRepository。为此,请在此处阅读有关 IoC 容器的内容:http://laravel.com/docs/5.0/container

    您的控制器中的构造将是这样的:

    public function __construct(ArticleRepository $articles)
    {
        $this->articles = $articles;
    }
    

    总而言之,当您需要在控制器中获取所有文章时,只需执行以下操作:

    public function index()
    {
        $articles = $this->articles->all();
        return View::make('articles.index')->with(['articles' => $articles]);
    }
    

    通过这种做法,您将拥有一个干净的应用程序,其中包含可测试控制器以及漂亮的组织和设计。 ;)

    听着,我尽量让你理解这个概念。使用存储库不仅仅是一种方法。所以我让cmets中的链接。并在此处提供其他参考。
    我相信你很快就会明白。
    学习成功! :)

    https://laracasts.com/search?q=repositories&q-where=lessons
    http://ryantablada.com/post/the-repository-pattern-in-action
    http://culttt.com/2014/03/17/eloquent-tricks-better-repositories/
    http://culttt.com/2013/07/15/how-to-structure-testable-controllers-in-laravel-4/

    【讨论】:

    • 感谢您对此的明确解释。我也会检查你的参考链接。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-09
    • 1970-01-01
    • 2020-11-22
    • 2013-09-18
    相关资源
    最近更新 更多