Skip to content

Commit

Permalink
Merge pull request #924 from LuckyCyborg/master
Browse files Browse the repository at this point in the history
Make the Routing configurable via app/Config/Routing.php
  • Loading branch information
daveismynamecom committed Jun 2, 2016
2 parents 170ce2d + 5d30c18 commit be5e5d4
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 9 deletions.
5 changes: 5 additions & 0 deletions app/Config.example.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@
*/
require 'Config/App.php';

/**
* Setup the Routing configuration
*/
require 'Config/Routing.php';

/**
* Setup the Language configuration
*/
Expand Down
24 changes: 24 additions & 0 deletions app/Config/Routing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Routing Configuration
*
* @author Virgil-Adrian Teaca - [email protected]
* @version 3.0
*/

use Core\Config;


/**
* Routing configuration
*/
Config::set('routing', array(
'patterns' => array(
//':hex' => '[[:xdigit:]]+',
//':uuidV4' => '\w{8}-\w{4}-\w{4}-\w{4}-\w{12}'
),
'default' => array(
'controller' => DEFAULT_CONTROLLER,
'method' => DEFAULT_METHOD
)
));
8 changes: 7 additions & 1 deletion app/Modules/Users/Models/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,26 @@

namespace App\Modules\Users\Models;

use Core\Config;
use Auth\GenericUser;
use Database\Model as BaseModel;


class Users extends BaseModel
{
protected $table = 'users';
protected $table = null;

protected $primaryKey = 'id';


public function __construct()
{
parent::__construct();

// Configure the Model's table.
if($this->table === null) {
$this->table = Config::get('auth.table');
}
}

public function updateGenericUser(GenericUser $user)
Expand Down
13 changes: 10 additions & 3 deletions system/Routing/ClassicRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Core;

use Core\Config;
use Helpers\Inflector;
use Helpers\Url;

Expand Down Expand Up @@ -88,7 +89,7 @@ public function dispatch()

// Search the defined Routes for any matches; invoke the associated Callback, if any.
foreach ($this->routes as $route) {
if ($route->match($uri, $method, false)) {
if ($route->match($uri, $method)) {
// Found a valid Route; process it.
$this->matchedRoute = $route;

Expand Down Expand Up @@ -143,6 +144,12 @@ public function dispatch()
*/
public function autoDispatch($uri)
{
// Retrieve the options from configuration.
$options = Config::get('routing.default', array(
'controller' => DEFAULT_CONTROLLER,
'method' => DEFAULT_METHOD
));

// Explode the URI to its parts.
$parts = explode('/', trim($uri, '/'));

Expand Down Expand Up @@ -190,12 +197,12 @@ public function autoDispatch($uri)
}

// Get the normalized Controller.
$defaultOne = !empty($moduleName) ? $moduleName : DEFAULT_CONTROLLER;
$defaultOne = !empty($moduleName) ? $moduleName : $options['controller'];

$controller = !empty($controller) ? $controller : $defaultOne;

// Get the normalized Method.
$method = !empty($parts) ? array_shift($parts) : DEFAULT_METHOD;
$method = !empty($parts) ? array_shift($parts) : $options['method'];

// Prepare the Controller's class name.
$controller = str_replace(array('//', '/'), '\\', 'App/'.$basePath.$directory.$controller);
Expand Down
21 changes: 17 additions & 4 deletions system/Routing/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public function applyFilters()
* @return bool Match status
* @internal param string $pattern URL pattern
*/
public function match($uri, $method, $optionals = true)
public function match($uri, $method, $optionals = false, array $patterns = array())
{
if (! in_array($method, $this->methods)) {
return false;
Expand All @@ -165,14 +165,27 @@ public function match($uri, $method, $optionals = true)
return true;
}

//
// Build the regex for matching.
if (strpos($this->pattern, ':') !== false) {
$regex = str_replace(array(':any', ':num', ':all'), array('[^/]+', '[0-9]+', '.*'), $this->pattern);

if (strpos($this->pattern, '{') !== false) {
// Convert the Named Patterns to (:any), e.g. {category}
$regex = preg_replace('#\{([a-z]+)\}#', '(:any)', $this->pattern);

// Convert the Named Patterns to (:num), e.g. {:id}
$regex = preg_replace('#\{:([a-z]+)\}#', '(:num)', $regex);
} else {
$regex = $this->pattern;
}

if ($optionals) {
if (strpos($regex, ':') !== false) {
$searches = array_merge(array(':any', ':num', ':all'), array_keys($patterns));
$replaces = array_merge(array('[^/]+', '[0-9]+', '.*'), array_values($patterns));

$regex = str_replace($searches, $replaces, $regex);
}

if ($optionals && (strpos($regex, '(/') !== false)) {
$regex = str_replace(array('(/', ')'), array('(?:/', ')?'), $regex);
}

Expand Down
6 changes: 5 additions & 1 deletion system/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Routing;

use Core\Config;
use Helpers\Inflector;
use Helpers\Url;
use Routing\BaseRouter;
Expand Down Expand Up @@ -226,6 +227,9 @@ protected static function register($method, $route, $callback = null)
*/
public function dispatch()
{
// Retrieve the additional Routing Patterns from configuration.
$patterns = Config::get('routing.patterns', array());

// Detect the current URI.
$uri = Url::detectUri();

Expand All @@ -243,7 +247,7 @@ public function dispatch()
}

foreach ($this->routes as $route) {
if ($route->match($uri, $method)) {
if ($route->match($uri, $method, true, $patterns)) {
// Found a valid Route; process it.
$this->matchedRoute = $route;

Expand Down

0 comments on commit be5e5d4

Please sign in to comment.