redcatphp / mvc
Mvc - Model-View-Controller basics classes and modular routing helpers
This package has no released version yet, and little information is available.
README
Here is the MVC basics classes and modular routing helpers.
At first, if you're uncomfortable with this design pattern or if you have some certitudes based on common implementations, i recommand you to read Mvc in PHP tutorial.
These classes are very basic and was defined for extends purpose. They offer some conventional api, magic methods and interfaces implementation.
Model
A model is shared between a view and a controller and is responsible to handle data.
The view will use it to access some arbitrary setted data that coming from user http request, database, or any other source of information.
The controller will use it to store data in reponse of a user action from http request.
Here is somes example of main features:
class MyModuleModel extends \\RedCat\\Mvc\\Model{ private $customData; function getCustomData(){ return $this->customData; } function setCustomData($data){ $this->customData = $data; } } $getter = function($id)use($db){ return $db->getRow('table',$id); }; $setter = function($id,$value)use($db){ $db->setRow('table',$id,$value); }; $model = new MyModuleModel($getter,$setter); $model['foo'] = 'bar'; echo $model['foo']; // show 'bar' if(!$model->{21}){ // call $getter $model->{21} = ['foo'=>'bar']; // call $setter } $model(); // if invokable, invoke to end set
View
A view is independent of model or controller and is responsible of handle presentation of data and template render. It can be build with a template engine and have an access to model for extract data. In case it have a template engine it will be responsible for select appropriate template and pass it variables from model.
class MyModuleView extends \\RedCat\\Mvc\\View{ function assignViaCustomPresentation($vars){ // $this->model-> ... //... } } $view = new MyModuleView($model,$templateEngine); $view(); // invoke to render, it will invoke the templateEngine //or $view($template); // you can pass an optional template path to render
Controller
A controller is independent of model or view and is responsible of handle user action and it's consequences. Controller is an optional component in a module and the dictum is "keep controller as few as possible".
class MyModuleController extends \\RedCat\\Mvc\\Controller{ function action($posted){ // $this->model-> ... //... } } $controller = new MyModuleController($model); if(isset($\_POST['action'])){ $controller->action($\_POST['action']); } $controller(); // if invokable, invoke to handle $\_POST inside
Route
A route is a coupling of a Model, a View and optionally, a Controller or even a Template engine which will be passed to view.
$route = new \\RedCat\\Mvc\\Route($model,$view,$controller,$templateEngine); $route(); // will invoke controller (if invokable), then model (if invokable), and finally view to render
Group
A group is an extension of Route which will group MVC triad to a common namespace. With the following example, the triad will be MyModuleNamespace\Model, MyModuleNamespace\View and MyModuleNamespace\Controller. You can optionally pass it a template engine.
$group = new \\RedCat\\Mvc\\Group('MyModuleNamespace',$templateEngine); $group(); // then invoke it
Module
A module is an extension of group, the only difference is it will use a prefix for the triad class names.
//by default prefix is 'Module\\' $module = new \\RedCat\\Mvc\\Module('MyModuleNamespace',$templateEngine); //but you can override it $module = new \\RedCat\\Mvc\\Module('MyModuleNamespace',$templateEngine,'MyNamespaceForModules\\\\'); $module(); // then invoke it