A real alternative to singleton and service locators?

I'm trying to sharpen my programming skills, and I came across a frustrating problem, which is likely to be best explained with an example:

Let's say I create microCMS in PHP. This microSMS has a Router class that is responsible for routing. It also contains the URI and additional parameters that are extracted from it.

class Router{
  private $uri;
  private $params;
  ...
  public function getRoute(){ ... }
  ...
  public function getParams(){
    return $this->params;
  }
  ...
}

I also have a Front Controller to which I am passing a new Router () object. So far, so good, I can access additional options in my Front Controller (via $router->getParams();).

class FrontController{
  private $controller;
  private $view;

  public function __construct(Router $router){
    $route = $router->getRoute();
    ...
    $params = $router->getParams(); //Yay, I can get to the params here!
    ...
    $this->view = new View($route->getModel());
    ...
  }

, . - . , (, URI).

class View{
  public function output(){
    //But how do I access the Router params here...?
  }
}

, , , , Router static Router::getParams()... , -.

, - Router View. , , - . , , , , . ?

Locator - $serviceLocator->getRouter() . , -, -.

, ? - CMS?

+4
1

FrontController Injection Dependency, . , global -esque, Singletons.

, View , . , - . , o. ​​,

public function setRouter(Router $router) {
     $this->router = $router;
}

.

, .

+2

All Articles