pars / pars-mvc
This package's canonical repository appears to be gone and the package has been frozen as a result.
Requires
- php: ^8.0
- pars/pars-beans: @dev
- pars/pars-helpers: @dev
- pars/pars-patterns: @dev
Requires (Dev)
- phpunit/phpunit: ^9.5
- dev-master
- v0.2.57
- v0.2.56
- v0.2.55
- v0.2.54
- v0.2.53
- v0.2.52
- v0.2.51
- v0.2.50
- v0.2.49
- v0.2.48
- v0.2.47
- v0.2.46
- v0.2.45
- v0.2.44
- v0.2.43
- v0.2.42
- v0.2.41
- v0.2.40
- v0.2.39
- v0.2.38
- v0.2.37
- v0.2.36
- v0.2.35
- v0.2.34
- v0.2.33
- v0.2.32
- v0.2.31
- v0.2.30
- v0.2.29
- v0.2.28
- v0.2.27
- v0.2.26
- v0.2.25
- v0.2.24
- v0.2.23
- v0.2.22
- v0.2.21
- v0.2.20
- v0.2.19
- v0.2.18
- v0.2.17
- v0.2.16
- v0.2.15
- v0.2.14
- v0.2.13
- v0.2.12
- v0.2.11
- v0.2.10
- v0.2.9
- v0.2.8
- v0.2.7
- v0.2.6
- v0.2.5
- v0.2.4
- v0.2.3
- v0.2.2
- v0.2.1
- v0.2.0
- v0.1.47
- v0.1.46
- v0.1.45
- v0.1.44
- v0.1.43
- v0.1.42
- v0.1.41
- v0.1.40
- v0.1.39
- v0.1.38
- v0.1.37
- v0.1.36
- v0.1.35
- v0.1.34
- v0.1.33
- v0.1.32
- v0.1.31
- v0.1.30
- v0.1.29
- v0.1.28
- v0.1.27
- v0.1.26
- v0.1.25
- v0.1.24
- v0.1.23
- v0.1.22
- v0.1.21
- v0.1.20
- v0.1.19
- v0.1.18
- v0.1.17
- v0.1.16
- v0.1.15
- v0.1.14
- v0.1.13
- v0.1.12
- v0.1.11
- v0.1.10
- v0.1.9
- v0.1.8
- v0.1.7
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
- v0.0.4
- v0.0.3
- v0.0.2
- v0.0.1
- dev-develop
This package is auto-updated.
Last update: 2022-11-30 23:33:21 UTC
README
This library provides MVC implementation for PARS Framework.
Understanding:
Model: Is responsible for loading and saving data via bean finder and processor. Methods for loading additional data may also be added.
Controller: Initializes view with data provided by the model. Controller actions can also be nested.
View: Renders a template or builds html via the provided object oriented html builder.
Installation
Run the following to install this library:
$ composer require pars/pars-mvc
In your routs add:
$app->any(\Pars\Mvc\Handler\MvcHandler::getRoute(), \Pars\Mvc\Handler\MvcHandler::class, 'mvc');
Registering Controllers and Models:
Configuration example in your Application
'mvc' => [ 'error_controller' => 'index', 'controllers' => [ 'index' => \Pars\Admin\Index\IndexController::class, ], 'models' => [ 'index' => \Pars\Admin\Index\IndexModel::class, ], ],
This will register IndexController
under the path /index
.
Implementing controllers and models:
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'; } }
Layout and Component
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); } }
Documentation
Browse the documentation online at https://docs.parsphp.org/pars-mvc/