2

Goal: understand whether this implemented order/logic of operation indeed belongs to the C part of MVC.

Situation: I have nearly finished a simple note-taking site with markdown files. No database is used except for authentication. The lack of database, however, makes it difficult to know if I am neglecting the M of MVC.

Because I did not want to have the .md extension as part of the pretty url, I am heavily relying on the PageController to settle the order of operation.

From the Controller, PageController inherits the constructed filesystem/Flysystem(fs), twig, and the "$app" that processes any of the three scenarios.

It first checks to see if a param.md exists and then whether param is a directory. If none of the above, then it's a new note. In each case, it uses a corresponding method set by the Application ($app) that processes and returns an array (title, breadcrumbs, content/directory listing, new template etc).

<?php 
namespace App\Controller;
class PageController extends Controller {

public function Page($param){
  $file=$param.'.md';
  if ($this->fs->has($file)) {
    $data=$this->app->setNote($file);
    return $this->app->render('note.twig',$data)
  }
  elseif ($this->fs->has($param)) {
    $data=$this->app->setFolder($param);
    return $this->app->render('folder.twig',$data)
  }
  else {
    $data=$this->app->setNew($param);
    return $this->app->render('new.twig',$data)
   }  
 }
}

Per "PHP the Right Way":

Controllers handle the request, process the data returned from models and load views to send in the response.

Although my code works and gets the job done, it does not appear to the be right way because the main App is doing the processing. I guess I could just move the Application.php in the Models folder, but would that make it adhere to "right way"? Should I use a Middleware before the PageController gets to work?

It may seem silly to ask about a code that just works, but my goal is to better understand/learn the current wisdom's ways when dealing with flat-files.

farhang
  • 407
  • 1
  • 5
  • 13
  • Quick side-note: I am not using any (micro)framework to force myself to understand each of the php cogs. – farhang Dec 18 '17 at 00:03

1 Answers1

-1

Regardless of whether you are 'database-less', the data is being stored / accessed in the .md files.

Access to them should be abstracted to a Model. You should create a File.find object + method, and/or a File.find_or_create. Then

$file = File.find_or_create($param);
$render_type = $file.type . '.twig';
return $this->app->render($render_type, $file.data);

Put all your if logic in the Model.

New Alexandria
  • 6,951
  • 4
  • 57
  • 77