Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.