MVC Refactor, Take 2

I like it! this is pretty much what I was going for other than you’ve broken apart the router a lot. My biggest issue with this approach is that although I’m all for convention over configuration when you only have convention it’s rather limiting. How would I, for example:

  • Have another page on /clock-ntp that used the same template/controller and used a different model (one that connected to an NTP server to get the time)

  • Have another page /date which used a different template but said “The date in $city is d/m/Y” using the same model

Either I’d have to create date.html.php and DateModel introducing redundancy (and extra work) or I’d need to have this logic twice, once for views and once for models?

public function buildClientModel()
        {
	    if ($this->request->path === '/clock-ntp') return new NtpModel;
            $className = ucfirst($this->request->object)."Model";
            $this->clientModel = ModelFactory::build($className);
            return $this->clientModel;
        }
```php
class View {

	private $template;
	private $model;
        public $headers;


	public function __construct(Request $request)
        {

        	if ($request->path === '/date') $this->template = 'date.html.php';
            else $this->template = $request->object . ".html.php";
	}

Obviously this would be configured using an array or some other data structure… although by doing this all that’s happened is you’ve moved the router logic into two places.

edit: To answer my own question and give some food for thought, I achieve this by having ‘modules’ rather than simple controllers and models so my folder structure now changes from:

clock.html.php
ClockModel.php

to

/clock/default.html.php
/clock/defaultmodel.php

(These can go in subdirectories but let’s keep it simple)

/clock/ then loads default.html.php and defaultmodel.php while /clock/ntp looks for a /clock/ntp.html.php and /clock/ntpmodel.php, if it can’t find the files it uses default. That way /clock/date uses /clock/date.html.php and /clock/defaultmodel.php while /clock/ntp uses /clock/ntpmodel.php and /clock/default.html.php . Of course this falls down and you still need proper routing rules if you want a better URL or you want to use ntpmodel.php with date.html.php How do I handle this? Each module e.g. /clock/ has its own routes.json and provides this information when it’s necessary :slight_smile: