Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > de.comp.lang.php > #3618
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Newsgroups | de.comp.lang.php |
| Subject | Re: Darstellung von Knoten mit Trennung zwischen view und Code |
| Date | 2015-12-19 18:33 +0100 |
| Organization | PointedEars Software (PES) |
| Message-ID | <16302867.nnWqlhfbJE@PointedEars.de> (permalink) |
| References | <n4uao3$v4r$1@news.albasani.net> <1t5672db4fi7736n3e8%sfroehli@Froehlich.Priv.at> <1840896.Q9fhR2YkPP@PointedEars.de> <n50cd0$r3b$1@news.albasani.net> |
Jan Novak wrote:
> Am 17.12.2015 um 20:45 schrieb Thomas 'PointedEars' Lahn:
>> $this ist bei MVC in Templates üblicherweise die Referenz auf die Instanz
>> der View-Klasse für das Template (denn dieses wird in einer View-Methode
>> – render() o.ä. – included), $group wären die darzustellenden Daten.
>> Wobei sich aber nicht das Template die Daten aus dem Model holen, oder
>> gar den Controller bemühen soll, sondern der Controller muss vorgängig
>> die Daten aus dem Model holen und damit eine möglichst
>> darstellungsunabhängige View- Variable definieren, deren Wert vom
>> Template über die View-Instanz benutzt wird:
>>
>> Controller <----> Model
>> ^ `-. .-' :
>> : X X
>> v .-' '-. :
>> View <--------- Template
>>
>
> klingt einfach und verständlich. Ist aber nicht so einfach umzusetzen,
> weil ich mir nicht sicher bin, wohin mit den Sachen ...
MVC-Gerüst (keine Kopie von PHPX, sondern möglichst einfach und trotzdem
sauber “from scratch”; kurz mit PHP 5.6 getestet):
Base.php
---------
<?php
namespace PointedEars\PHPX;
abstract class Base
{
public function __get ($name)
{
$getter = 'get' . ucfirst($name);
if (method_exists($this, $getter))
{
return $this->$getter();
}
if (property_exists($this, "_$name"))
{
return $this->{"_$name"};
}
throw new DomainException(
"No getter for property '$name' or no property '_$name'");
}
public function __set ($name, $value)
{
$setter = 'set' . ucfirst($name);
if (method_exists($this, $setter))
{
return $this->$setter($value);
}
if (property_exists($this, "_$name"))
{
return ($this->{"_$name"} = $value);
}
throw new DomainException(
"No setter for property '$name' or no property '_$name'");
}
}
Model.php
----------
<?php
namespace PointedEars\PHPX;
abstract class Model extends Base
{
public function __construct (array $data = null)
{
if (is_array($data))
{
foreach ($data as $key => $value) $this->$key = $value;
}
}
}
FooModel.php
-------------
<?php
namespace PointedEars\Demo;
class FooModel extends \PointedEars\PHPX\Model
{
protected $_blub;
/**
* Maps property read access <code>$this->foo</code>
* to <code>$this->_blub</code>
*/
protected function getFoo ()
{
return $this->_blub;
}
/**
* Maps property write access <code>$this->foo = …</code>
* to <code>$this->_blub = (string) …</code>
*
* @param string $value New value
*/
protected function setFoo ($value)
{
$this->_blub = $value;
}
}
View.php
---------
<?php
namespace PointedEars\PHPX;
abstract class View extends Base
{
/**
* Default template
*
* @var string
*/
protected $_template;
/**
* Template variables
*
* @var array
*/
protected $_vars = [];
/**
* Gets a template variable
*
* @param string $name
*/
public function getVar ($name)
{
if (array_key_exists($name, $this->_vars))
{
return $this->_vars[$name];
}
}
/**
* Sets a template variable
*
* @param string $name
* @param any $value
*/
public function setVar ($name, $value)
{
$this->_vars[$name] = $value;
return $this;
}
/**
* Renders the default template
*/
public function render ()
{
include 'layouts/' . $this->_template;
}
}
MyView.php
-----------
<?php
namespace PointedEars\Demo;
class MyView extends \PointedEars\PHPX\View
{
/**
* @var string
*/
protected $_template = 'index/foo.phtml';
}
foo.phtml
----------
<?= $this->getVar('foo') ?>
Controller.php
---------------
<?php
namespace PointedEars\PHPX;
abstract class Controller extends Base
{
public function __construct ()
{
$action = 'index';
if (isset($_GET['action']))
{
$action = $_GET['action'];
}
$this->{'_' . lcfirst($action) . 'Action'}();
}
}
IndexController.php
--------------------
<?php
namespace PointedEars\Demo;
class IndexController extends \PointedEars\PHPX\Controller
{
protected function _indexAction ()
{
/* You can do database access here */
$bar = isset($_GET['bar']) ? $_GET['bar'] : 'bar';
$model = new FooModel(['foo' => $bar]);
(new MyView())
->setVar('foo', $model->foo)
->render();
}
}
Application.php
----------------
<?php
namespace PointedEars\PHPX
function autoload ($class)
{
/* … */
}
spl_autoload_register(__NAMESPACE__ . '\\autoload');
abstract class Application
{
protected $namespace;
public function __construct ($namespace)
{
/* Set up defaults from configuration file */
$this->_namespace = $namespace;
}
public function run ($namespace)
{
/* Initialize session */
$controller = 'Index';
if (isset($_GET['controller']))
{
$controller = $_GET['controller'];
}
$controller = $namespace . '\\'
. ucfirst($controller) . 'Controller';
new $controller();
}
}
index.php
----------
<?php
namespace PointedEars\Demo
require 'Application.php';
class MyApplication extends \PointedEars\PHPX\Application
{
public function run ()
{
parent::run(__NAMESPACE__);
}
}
chdir('app');
(new MyApplication())->run();
Frohe Weihnachten :)
--
PointedEars
Zend Certified PHP Engineer
Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.
Back to de.comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar
Darstellung von Knoten mit Trennung zwischen view und Code Jan Novak <repcom@gmail.com> - 2015-12-17 13:44 +0100
Re: Darstellung von Knoten mit Trennung zwischen view und Code Stefan+Usenet@Froehlich.Priv.at (Stefan Froehlich) - 2015-12-17 16:01 +0000
Re: Darstellung von Knoten mit Trennung zwischen view und Code Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-12-17 20:45 +0100
Re: Darstellung von Knoten mit Trennung zwischen view und Code Stefan+Usenet@Froehlich.Priv.at (Stefan Froehlich) - 2015-12-17 22:27 +0000
Re: Darstellung von Knoten mit Trennung zwischen view und Code Jan Novak <repcom@gmail.com> - 2015-12-18 08:25 +0100
Re: Darstellung von Knoten mit Trennung zwischen view und Code Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-12-19 18:33 +0100
Re: Darstellung von Knoten mit Trennung zwischen view und Code Jan Novak <repcom@gmail.com> - 2016-01-11 14:08 +0100
csiph-web