IoC Container PHP Routing

158 Views Asked by At

I have recently started working on my own MVC PHP application. Currently I am trying to set everything up in a way that I would consider "clean". After some reading I came across IoC containers and decided that I wanted to use one. I am using Auryn for this.

Now I came across a problem in my router. In my router I create the objects of the models, views or controllers that I need, depending on a pre-defined Route datastructure. It looks like this:

$this->routes["overview"] = new Route("AboutModel",
                                     "MainOverviewView",
                                     "MainOverviewController");

The names defined in this array are basically class names. Those classes should be instantiated for the route "overview". Later in the __construct() of my FrontController I do this to create my classes:

public function __construct(Router $router, $routeName = "", $action = null) {
    $route = $router->getRoute($routeName);
    $model = new $route->model;
    $this->controller = new $route->controller($model);
    $this->view = new $route->view($model, $routeName);

    if (!empty($action)) {
        $this->controller->{$action}();
    }
}

$route->model would give me the name of the exact model class that I have to initialize for that route. So when sticking to the overview route this would give me $model = new AboutModel (Dont mind the names those were just to mess around). This seems something I should do using my IoC container, therefore I would rather have the IoC container create my needed objects.

But how should the container know what classes I need it to create?

So I am looking for a way to somehow dynamically create the needed Controller, Views and Models using the IoC container. I have made some research on this and I have found a "solution", which is passing the IoC Container into the constructor of the FrontController. I do not think that this is the correct way of doing this, as I believe that my objects should not know my IoC container.

Is there a "pretty" way of accomplishing what I want to accomplish?

If there is anything unclear please ask! Thank you!

EDIT: Workaround

I have created a work-around for that problem. Instead of having all those parameters in my FrontPatterns constructor I am instead now passing the needed controller and view in via a set method.

This generates a little bit of initialization code that runs everytime the user changes the site. I put all of that into my index.php file and it looks like this:

$routeName = isset($_GET['route']) ? $_GET['route'] : "";
$router = $injector->make("Router");
$componentNames = $router->getRoute($routeName);
$frontController = $injector->make("FrontController");
$frontController->setController($injector->make($componentNames->controller));
$frontController->setView($injector->make($componentNames->view));

echo $frontController->output();

I think I am going to stick to that solution for the moment (unless someone has a better way of doing this :-))

0

There are 0 best solutions below