Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2274aea

Browse files
Add namespace to the routes
1 parent 6a915cf commit 2274aea

File tree

5 files changed

+65
-19
lines changed

5 files changed

+65
-19
lines changed

‎src/app/Router.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ public function addRoute(string $route, array $params = []) : void
3838
$route = preg_replace('/\//', '\\/', $route);
3939

4040
// Convert variables e.g. {controller}
41-
$route = preg_replace('/\{([a-z]+)\}/', '(?P<1円>[a-z-]+)', $route);
41+
$route = preg_replace('/\{([a-z0-9]+)\}/', '(?P<1円>[a-z0-9-]+)', $route);
4242

4343
// Convert variables with custom regular expressions e.g. {id:\d+} for numbers
44-
$route = preg_replace('/\{([a-z]+):([^\}]+)\}/', '(?P<1円>2円)', $route);
44+
$route = preg_replace('/\{([a-z0-9]+):([^\}]+)\}/', '(?P<1円>2円)', $route);
4545

4646
// Add start and end delimeter
4747
$route = '/^' . $route . '$/i';
@@ -89,12 +89,13 @@ public function setParams(string $uri) : void
8989
* If so, then unset 'controller' and 'action', which leaves $this->params = ['id' => 1]
9090
* And last, the desired function is called with all the parameters (array) like 'id'...
9191
* ... and this can be used or not if the method doesn't receive any arguments
92+
* @return void
9293
*/
9394
public function redirectTo() : void
9495
{
95-
$controller = '\\Controllers\\' . $this->params['controller'];
96+
$controller = $this->getNamespace() . $this->params['controller'];
9697
$action = $this->params['action'];
97-
98+
9899
if (class_exists($controller))
99100
{
100101
$controller = new $controller;
@@ -116,4 +117,20 @@ public function redirectTo() : void
116117

117118
call_user_func_array([$controller, $action], [$this->params]);
118119
}
120+
121+
/**
122+
* Get the namespace for the requested controller class
123+
* @return string $namespace String for the complete namespace
124+
*/
125+
private function getNamespace() : string
126+
{
127+
$namespace = '\\Controllers\\';
128+
129+
if (array_key_exists('namespace', $this->params))
130+
{
131+
$namespace .= $this->params['namespace'] . '\\';
132+
}
133+
134+
return $namespace;
135+
}
119136
}

‎src/app/routes.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
<?php
22

3+
// Static pages routes
34
$router->addRoute('', ['controller' => 'Index', 'action' => 'home']);
45
$router->addRoute('about', ['controller' => 'Index', 'action' => 'about']);
6+
7+
// Routes in main controllers/ folder (Namespace \Controllers)
58
$router->addRoute('{controller}/{action}');
69
$router->addRoute('{controller}/{action}/{id:\d+}');
710
$router->addRoute('{controller}/{id:\d+}/{action}');
11+
12+
// Routes in folder controllers/module1/ (Namespace \Controllers\Module1)
13+
$router->addRoute('module1/{controller}/{action}', ['namespace' => 'Module1']);
14+
$router->addRoute('module1/{controller}/{id:\d+}/{action}', ['namespace' => 'Module1']);
15+
16+
// Routes in folder controllers/module2/ (Namespace \Controllers\Module2)
17+
$router->addRoute('module2/{controller}/{action}', ['namespace' => 'Module2']);
18+
$router->addRoute('module2/{controller}/{id:\d+}/{action}', ['namespace' => 'Module2']);
19+
820
$router->setParams(getUri());

‎src/controllers/Module1/Class1.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
namespace Controllers\Module1;
3+
4+
use \Controllers\Controller;
5+
6+
/**
7+
* ModuleClass file
8+
* For example for large projects with many folders (modules)
9+
*/
10+
class Class1 extends Controller
11+
{
12+
public function index()
13+
{
14+
echo 'This is ' . __CLASS__;
15+
}
16+
}

‎src/controllers/Module1/Class2.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
namespace Controllers\Module1;
3+
4+
use \Controllers\Controller;
5+
6+
/**
7+
* ModuleClass file
8+
* For example for large projects with many folders (modules)
9+
*/
10+
class Class2 extends Controller
11+
{
12+
public function index()
13+
{
14+
echo 'This is ' . __CLASS__;
15+
}
16+
}

‎src/controllers/Task.php

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /