douxun4860 2019-06-23 06:11
浏览 39

php Mvc Structure中的新类实例

I working with own project using MVC structure and autoload class using PSR-4 using composer. I choose Php Fastroute library for my router engine.

My Structure Is:

enter image description here

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();

Controller:

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;

    }
}

IndexController:

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
    }
}

In action I put class new instance(library and core class ie: Config or Ducoment or Templates or many more if need ...) in __construct() Of Bridge Controller and extends IndexController. this model work for me But i dont know this method is true Or false?! Actually, I need to load Object (new instance) From Core class to my IndexController But I dont know where will it be placed(better and ture)?! (in Base Controller - In IndexController - In Core.php and require - In Index.php)??

  • 写回答

1条回答 默认 最新

  • dtsfnyay300457 2019-06-23 09:40
    关注

    I would like to give you some ideas how to structure a MVC 2 web application in PHP:

    Directory structure

    Use PHP (FIG) standards like

    • Modern coding style (PSR-1, PSR-2, PSR-12)
    • PHPDoc Standard (PSR-5, PSR-19)
    • Class Autoloader (PSR-4)
    • HTTP request and response (PSR-7)
    • HTTP Server Request Handlers, Middleware (PSR-15)
    • HTTP Factories (PSR-17)
    • Dependency injection container (PSR-11): league/container

    Other useful tips and libraries:

    • Use a router: I would recommend league/route (based on fastroute)
    • Single action controllers (ADR)
    • Logging (PSR-3): monolog/monolog
    • Database Migrations: Phinx
    • Date and time handling: Chronos
    • Console Commands: Symfony/console
    • Unit testing: PHPUnit

    To your question:

    • Don't use inheritance (extend) anymore (if possible), use composition (dependency injection) instead.
    • Use construtor injection
    • Don't create the instances yourself, let the dependency injection container (DIC) handle this task.
    • Make your classes "final" by default (except Repository classes)
    • Your controller should be only handle one thing (SRP). A single action controller would be more SOLID compliant.
    • Are you sure that all controller properties must be public?
    • The term "Model" sounds like the "Active Record Anti-pattern" to me. Maybe consider "Repositories" as a better solution.
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题