0

I've trying to expand my knowledge on separation of concerns in php. I've been practicing for over a year I think and trying to write my own mvc framework for practice. Now I am stuck again in routing and on how to initiate MVC triad.

I have this uri that I want to map so I can identify which controller and which view to use

$uri = filter_var(rtrim(filter_input(INPUT_GET, 'url', FILTER_SANITIZE_STRING), '/'), FILTER_SANITIZE_URL);

lets say this piece of code resides in my bootstrap.php file that acts as the entry point.

While reading Tom Butler's Blog I realize many things, like views should have access to models, but not quite, using a viewmodel is better, or just a model.

I've come across his IOC or his Dependency Injection Container and fell interested on trying it.

What lacks in that article is the dispatching part, which I am very interested to learn, I've tried a couple things to make it work but with no avail.

I wanted to implement this because I want a single call of controller can have shared dependency across its views, something like

$route = $router->getRoutes(); // maybe something that return a Route object with Controller, and View object that has already shared dependecies.

I do not know if my understanding on the above paragraphs where correct and if really can be of use on my routing. Correct me if I am wrong.

The real question is how would the dispatcher looks like? and if I were to use the convention over configuration stuff of mr. tom, should I declare the routes individually in my bootstrap? Like these

$dice->addRule('$route_user/edit', $rule);
$dice->addRule('$route_user/delete', $rule);
...

I wonder if I could just do:

$controller->method($params)

After I have settled on what view and controller I needed.

Joey Hipolito
  • 3,108
  • 11
  • 44
  • 83

1 Answers1

0

I'm not sure that this answer will be what you want, but if I where you I'll do it this way, it will be more restFull, and more "convention over configuration":

  1. Use http verbs for edit , delete...
  2. Use a standard way to handle your urls: "article/", "article/2" Where article is your controller name AND your view name
  3. Use a simple array tree for simple routes:

    $bootstrap=[ ... "routes"=>[ "myFirstCategory"=>[ "art"=>"article", ... ], "mySecondCategory"=>[ ... This way, myFirstCategory/art can be redirected to article controller and view, with a recursive function that handle the routes tree

  4. In this tree you can use a callback rule for complex rules (that callback have to be handeled by your recursive function for the routes tree:

    ... "art"=>function($myContainer){...return ['view'=>$view,'controller'=>$controller,'id'=>$id...];}...

It's just some ideas to make it more easy to use...

Pierre
  • 429
  • 3
  • 15