pars/pars-mvc

该软件包的规范存储库似乎已丢失,因此该软件包已被冻结。


README

Build Status Coverage Status

这个库为PARS框架提供了MVC实现。

理解

模型:负责通过bean查找器和处理器加载数据和保存数据。也可以添加加载额外数据的方法。

控制器:初始化视图,使用模型提供的数据。控制器动作也可以嵌套。

视图:通过提供的面向对象的HTML构建器渲染模板或构建HTML。

安装

运行以下命令来安装此库

$ composer require pars/pars-mvc

在您的路由中添加

$app->any(\Pars\Mvc\Handler\MvcHandler::getRoute(), \Pars\Mvc\Handler\MvcHandler::class, 'mvc');

注册控制器和模型

在您的应用程序中的配置示例

    'mvc' => [
        'error_controller' => 'index',
        'controllers' => [
            'index' => \Pars\Admin\Index\IndexController::class,
        ],
        'models' => [
            'index' => \Pars\Admin\Index\IndexModel::class,
        ],

    ],

这将在路径/index下注册IndexController

实现控制器和模型

class IndexController extends \Pars\Mvc\Controller\AbstractController {
    
    protected function initView(){
        $view = new MyView();
        $view->setLayout(new MyLayout());
        $this->setView($view);
        
    }
    
    protected function initModel(){
        $this->getModel()->initialize();
        $this->getModel()->initializeDependencies();
    }
      
    public function indexAction()
    {
        $this->getView()->set('heading', $this->getModel()->getHeading());
        $this->getView()->set('text', $this->getModel()->getText());
        // adding compontent to be rendered
        $this->getView()->pushComponent(new MyCompontent());
        // nesting additional controller action to be rendered e.g. UserController::indexAction
        // all compontents of the nested controller action will be appended to the parent controllers view
        // rendering templates is currently not supported for nested controller actions
        $this->pushAction('user', 'index');
    }
}
class IndexModel extends \Pars\Mvc\Model\AbstractModel {
    public function getHeading(): string 
    {
        return 'Hello World';
    }
    
      public function getText(): string 
    {
        return 'Hello Hello Hello Hello';
    }
}

布局和组件

class MyLayout extends \Pars\Mvc\View\AbstractLayout {
    protected function initialize() {
      parent::initialize();
      $this->setTag('html');
      $head = new \Pars\Mvc\View\ViewElement('head');
      $this->initHead($head);
      $this->push($head);
      $body = new \Pars\Mvc\View\ViewElement('body');
      $this->initBody($body);
      $this->push($body);
    }
    protected function initHead(\Pars\Mvc\View\ViewElement $head) {
       $link = new \Pars\Mvc\View\ViewElement('link');
       $link->setAttribute('rel', 'stylesheet');
       $link->setAttribute('href', 'styles.css');
       $head->push($link);
    }
    
    protected function initBody(\Pars\Mvc\View\ViewElement $body) {
        $heading = new \Pars\Mvc\View\ViewElement('h1');
        $heading->setContent('{heading}');
        $body->push($heading);
    }
}
class MyComponent extends \Pars\Mvc\View\AbstractComponent {
    
    protected function initialize() {
      parent::initialize();
      $text = new \Pars\Mvc\View\ViewElement('p');
      $text->setContent('{text}');
      $this->push($text);
    }
    
}

文档

在线浏览文档https://docs.parsphp.org/pars-mvc/

支持