Jump to content

mvc routing design advice


Destramic

Recommended Posts

hey guys im looking for design advice on my framework im trying to design...but im a bit stuck on how the routing should work.

 

the code you will see below...basically you can create a route in the bootstrap and when the framework is executed to run it will see if the uri is a match to a route....now this is were i dunno what to do.

 

1. should the routing system pass the controller/action and parameters to the request then passed to the dispatcher for dispatching

 

2. or should the controller/action and parameters be passed straight to the dispatcher to be dispatched?

 

well if someone could help me on which design pattern would be better and give me some advise i'd be very happy...thanks

 

<?php

class Router
{
protected $_uri_variable = ":";
protected $_routes       = array();
protected $_request      = null;

public function __construct()
{

}

public function add_route($pattern, array $route)
{
	$this->_routes[$pattern] = $route;
}

public function is_route($uri)
{
	$routes = $this->_routes;

	foreach ($routes as $pattern => $route)
	{
		if (array_key_exists('controller', $route)) 
		{
			$controller = $route['controller'];
			unset($route['controller']);
		}
		if (array_key_exists('action', $route))
		{
			$action = $route['action'];
			unset($route['action']);
		}

		$parameters = $route;

		if (preg_match_all('/(?<=\w+/', $pattern, $matches))
		{
			foreach ($matches as $match)
			{	
				$count = count($matches);

				for ($i = 0; $i < $count;)
				{
					$find = $match[$i];
					$i++;
				}

				if (array_key_exists($find, $parameters)) 
				{
					$value = $parameters[$find];

					if (substr($value, 0, 1) == ':')
					{
						// get from request parameters;
						//$this->_request->get_parameter($find);
					}
					else
					{
						$pattern = str_replace(':' . $find, $value, $pattern);
					}
				}
			}
		}	

		if ($uri == $pattern)
		{
			return true;
		}		
		else 
		{
			return false;
		}	
	}
}
}

 

$router = $front_controller->get_router();
$router->add_route('game/:game/leagues', array('controller' => 'leagues',
                                               'action'     => 'leagues',
                                               'game'       => 'counter-strike'));

Link to comment
https://forums.phpfreaks.com/topic/239649-mvc-routing-design-advice/
Share on other sites

It's all up to you really. In my framework, I have the router create a command object. This command object is basically just a placeholder for the names of the module, controller & action foe the current request. I then pass this into my request object (it's going to need it anyway), which in turn passes it to the dispatch process.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.