【问题标题】:Class new Instance in php MVC Structurephp MVC结构中的类新实例
【发布时间】:2019-06-23 06:11:58
【问题描述】:

我使用 MVC 结构 处理自己的项目,并使用 composer 使用 PSR-4 自动加载类。我为我的路由器引擎选择 Php Fastroute 库。

我的结构是:

index.php

define('DS', DIRECTORY_SEPARATOR, true);
define('BASE_PATH', dirname(__DIR__) . DS, TRUE);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require '../vendor/autoload.php';
require '../application/core/Core.php';

// Our framework is now handling itself the request
$app = new Framework\Core();

$response = $app->handle();

控制器:

namespace App\Core;

class Controller
{

    public $templates;

    public function __construct()
    {

        // My Question
        $this->config = new \App\Core\Setting();
        $this->Language = new Language('en');
        $this->Url = new \App\Core\Url(Config::get('URL'),Config::get('URL'));
        $this->templates = new \League\PlatesEngine(Config::get('PATH_VIEW'));
        $this->Document = new \App\Core\Document();
    }

    public function loadModel($name, $path = null) {

        $path = ($path === null) ? 'Catalog' : $path;

        $path = '\App\\'.$path.'\Model\\'.$name;

        $this->model = new $path;

        return $this->model;

    }

    public function loadController($name) {

        $path = '\App\Catalog\Controller\\'.$name;

        $this->controller = new $path;

        return $this->controller;

    }
}

索引控制器:

namespace App\Catalog\Controller\Home;

class IndexController extends \App\Core\Controller
{
    public function index()
    {
        $add['power'] = $this->config->get('on');
        $data['title'] = $this->Language->get('text_title');
        $this->templates->addData($data, 'common/header');
        //.... More Code
    }
}

在行动中,我将类新实例(库和核心类,即:Config 或 Ducoment 或 Templates 或更多,如果需要...)放在 Bridge Controller__construct() 中并扩展 IndexController。这个模型对我有用但我不知道这个方法是真是假?!实际上,我需要将对象(新实例)从核心类加载到我的IndexController 但我不知道它会放在哪里(更好和真实)?! (在Base Controller - 在IndexController - 在Core.php and require - 在Index.php)??

【问题讨论】:

  • 至少可以在 index.php 中新建配置、url 和语言,然后传递给 $app = new Framework\Core($config,$url,$language);
  • 这个问题没有正确或错误的答案。最好的方法是什么,这是一个见仁见智的问题。没有“PHP MVC 结构”之类的东西。
  • @jrswgtr:你的意思是什么:There is no such thing as "The PHP MVC structure"
  • MVC 结构是 PHP 本身不存在的东西。人们在构建框架或应用程序时自己实现它。这意味着没有完全错误或正确的方法来做到这一点。有更好和更坏的方法,但这是见仁见智的问题。
  • @jrswgtr:当然,我添加这个问题是为了分享更好的想法(更好和更坏的方式)。

标签: php model-view-controller


【解决方案1】:

我想告诉你一些如何在 PHP 中构建 MVC 2 网络应用程序的想法:

目录结构

使用 PHP (FIG) 标准,如

  • 现代编码风格(PSR-1、PSR-2、PSR-12)
  • PHPDoc 标准(PSR-5、PSR-19)
  • 类自动加载器 (PSR-4)
  • HTTP 请求和响应 (PSR-7)
  • HTTP 服务器请求处理程序,中间件 (PSR-15)
  • HTTP 工厂 (PSR-17)
  • 依赖注入容器 (PSR-11):联盟/容器

其他有用的提示和库:

  • 使用路由器:我会推荐联赛/路由(基于 fastroute)
  • 单动作控制器 (ADR)
  • 日志记录 (PSR-3):独白/独白
  • 数据库迁移:Phinx
  • 日期和时间处理:Chronos
  • 控制台命令:Symfony/console
  • 单元测试:PHPUnit

你的问题:

  • 不再使用继承(扩展)(如果可能),改用组合(依赖注入)。
  • 使用构造函数注入
  • 不要自己创建实例,让依赖注入容器 (DIC) 处理此任务。
  • 默认情况下使您的类“最终”(存储库类除外)
  • 您的控制器应该只处理一件事 (SRP)。单个动作控制器将更符合 SOLID。
  • 您确定所有控制器属性都必须公开吗?
  • 对我来说,“模型”一词听起来像是“Active Record 反模式”。或许可以考虑将“存储库”作为更好的解决方案。

【讨论】:

    猜你喜欢
    • 2016-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多