MVC: Instantiate Controller In Router?

Ralph

I'm trying to figure out if its bad practice to initiate a controller from within a router class. From what little I have been able to find about this, some say that the router shouldn't handle instantiating controllers. Below is how I started to develop my router class.

Example(note I'm omitting alot for the sake of typing.)

class Router {
 private $url, $controller;
 public function __construct($url)
 {
  $this->url = $url;
  $this->map(); /* maps url to controller and action*/

  /*dispatch controller*/
  $this->dispatch();
  }

 private function dispatch()
 {
  $controller = new $this->controller();
  $controller->executeAction();
  }

 }
ArtisticPhoenix

To answer you question I'd so no it violates separation of concerns. The router shouldn't be worried what controller handles it's request, or rather how that controller came into being. It only needs to know that at some point, even in the event of a 404 that some controller will handle it.

Now injecting a controller into the route would be ok, then you could prototype it as an interface like so,

public function dispatch(ControllerInterface $Controller){
   .....
}

otherwise you have to much hard linking, to much dependency, what if you need a second controller?

For example say you need an admin controller and a public controller, and a members controller. Do you then build 3 routers.

Personally the approach I am planning for a project I am working on is to use an event driven system, where there will be a group of controllers assigned by default as in a traditional routing system ( class/method/args... ) , say a controller folder will be searched for them. Other wise a controller will register itself to listen for a particular http request. So the flow is a bit like this.

Are there any registered listeners for this request, if no are there any controllers in our controller folder that match the routing schema, and at the very end is a 404 controller that will handle any request.

If any of these catch it the event ( that's being listened to is terminated ). The advantage of this over a purely hard wired route is, say I want to make a payment plugin, which needs a payment page, how do I put that in the controller folder as a third party vender? This way one only needs activate the plugin, which registers for the "payments" route, and listens within it's own package.

Maybe this is not a concern of you project but it's something to think about.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

iOS 7 - Failing to instantiate default view controller

From Dev

Isolated Controller Test can't instantiate Pageable

From Dev

Failed to instantiate the default view controller for UIMainStoryboardFile 'Main'

From Dev

Instantiate and Inject into AngularJS Controller

From Dev

Instantiate View Controller with Identifier with custom init

From Dev

Router resolve will not inject into controller

From Dev

Difference between controller and router?

From Dev

How to check existence of the controller in the storyboard before to instantiate it?

From Dev

Unable to find interface controller class 'InterfaceController' to instantiate

From Dev

PHP MVC DI practical exaple of Law of Demeter with Router, Controller and Model

From Dev

Xamarin Instantiate Initial Navigation Controller in Storyboard

From Dev

Separate router and controller in koa

From Dev

PHP MVC: use router inside controller?

From Dev

"Failed to instantiate module ui.router"

From Dev

would it be ok to create an instantiate an object in another controller?

From Dev

Instantiate View Controller not working

From Dev

Unable to instantiate Angular controller

From Dev

instantiate a controller while using dependency injection

From Dev

instantiate JPA controller by injecting from glassfish

From Dev

Difference between controller and router?

From Dev

Separate router and controller in koa

From Dev

Using Angular 1.4 new router with components http throws a warning "Could not instantiate controller HeadlinesController"

From Dev

Instantiate Swift View Controller in Objective C

From Dev

Instantiate View Controller not working

From Dev

Unable to instantiate Angular controller

From Dev

How to instantiate a view controller programatically, without storyboard

From Dev

How to instantiate a new view controller programmatically

From Dev

Automatically instantiate AngularJS controller nested in templateUrl

From Dev

How to Push a Navigation Controller after instantiate

Related Related

  1. 1

    iOS 7 - Failing to instantiate default view controller

  2. 2

    Isolated Controller Test can't instantiate Pageable

  3. 3

    Failed to instantiate the default view controller for UIMainStoryboardFile 'Main'

  4. 4

    Instantiate and Inject into AngularJS Controller

  5. 5

    Instantiate View Controller with Identifier with custom init

  6. 6

    Router resolve will not inject into controller

  7. 7

    Difference between controller and router?

  8. 8

    How to check existence of the controller in the storyboard before to instantiate it?

  9. 9

    Unable to find interface controller class 'InterfaceController' to instantiate

  10. 10

    PHP MVC DI practical exaple of Law of Demeter with Router, Controller and Model

  11. 11

    Xamarin Instantiate Initial Navigation Controller in Storyboard

  12. 12

    Separate router and controller in koa

  13. 13

    PHP MVC: use router inside controller?

  14. 14

    "Failed to instantiate module ui.router"

  15. 15

    would it be ok to create an instantiate an object in another controller?

  16. 16

    Instantiate View Controller not working

  17. 17

    Unable to instantiate Angular controller

  18. 18

    instantiate a controller while using dependency injection

  19. 19

    instantiate JPA controller by injecting from glassfish

  20. 20

    Difference between controller and router?

  21. 21

    Separate router and controller in koa

  22. 22

    Using Angular 1.4 new router with components http throws a warning "Could not instantiate controller HeadlinesController"

  23. 23

    Instantiate Swift View Controller in Objective C

  24. 24

    Instantiate View Controller not working

  25. 25

    Unable to instantiate Angular controller

  26. 26

    How to instantiate a view controller programatically, without storyboard

  27. 27

    How to instantiate a new view controller programmatically

  28. 28

    Automatically instantiate AngularJS controller nested in templateUrl

  29. 29

    How to Push a Navigation Controller after instantiate

HotTag

Archive