Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
U
UserAdminV2
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
庄欣
UserAdminV2
Commits
bc5299ab
Commit
bc5299ab
authored
Jul 29, 2011
by
Taylor Otwell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
tweaked router architecture to be non-static.
parent
7ecb2cf4
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
70 additions
and
46 deletions
+70
-46
system/router.php
+70
-46
No files found.
system/router.php
View file @
bc5299ab
...
@@ -3,77 +3,104 @@
...
@@ -3,77 +3,104 @@
class
Router
{
class
Router
{
/**
/**
* Search a set of routes for the route matching a method and URI.
* The request method and URI.
*
* @var string
*/
public
$request
;
/**
* All of the loaded routes.
*
* @var array
*/
public
$routes
;
/**
* Create a new router for a request method and URI.
*
*
* @param string $method
* @param string $method
* @param string $uri
* @param string $uri
* @return Route
* @param array $routes
* @return void
*/
*/
public
static
function
route
(
$method
,
$uri
)
public
function
__construct
(
$method
,
$uri
,
$routes
=
null
)
{
{
$routes
=
static
::
load
(
$uri
);
// Put the request method and URI in route form.
// Put the request method and URI in route form.
// Routes begin with the request method and a forward slash.
// Routes begin with the request method and a forward slash.
$
uri
=
$method
.
' /'
.
trim
(
$uri
,
'/'
);
$
this
->
request
=
$method
.
' /'
.
trim
(
$uri
,
'/'
);
// Is there an exact match for the request?
$this
->
routes
=
(
is_array
(
$routes
))
?
$routes
:
$this
->
load
(
$uri
);
if
(
isset
(
$routes
[
$uri
]))
{
return
Request
::
$route
=
new
Route
(
$uri
,
$routes
[
$uri
]);
}
}
foreach
(
$routes
as
$keys
=>
$callback
)
/**
{
* Create a new router for a request method and URI.
// Only check routes that have multiple URIs or wildcards.
*
// Other routes would have been caught by the check for literal matches.
* @param string $method
if
(
strpos
(
$keys
,
'('
)
!==
false
or
strpos
(
$keys
,
','
)
!==
false
)
* @param string $uri
{
* @param array $routes
foreach
(
explode
(
', '
,
$keys
)
as
$key
)
* @return Router
{
*/
if
(
preg_match
(
'#^'
.
static
::
translate_wildcards
(
$key
)
.
'$#'
,
$uri
)
)
public
static
function
make
(
$method
,
$uri
,
$routes
=
null
)
{
{
return
Request
::
$route
=
new
Route
(
$keys
,
$callback
,
static
::
parameters
(
$uri
,
$key
));
return
new
static
(
$method
,
$uri
,
$routes
);
}
}
}
}
}
}
/**
/**
* Load the appropriate route
file
for the request URI.
* Load the appropriate route
s
for the request URI.
*
*
* @param string $uri
* @param string $uri
* @return array
* @return array
*/
*/
public
static
function
load
(
$uri
)
public
function
load
(
$uri
)
{
{
$base
=
require
APP_PATH
.
'routes'
.
EXT
;
$base
=
require
APP_PATH
.
'routes'
.
EXT
;
return
(
is_dir
(
APP_PATH
.
'routes'
)
and
$uri
!==
''
)
?
array_merge
(
static
::
load_from_directory
(
$uri
),
$base
)
:
$base
;
if
(
!
is_dir
(
APP_PATH
.
'routes'
)
or
$uri
==
''
)
{
return
$base
;
}
list
(
$routes
,
$segments
)
=
array
(
array
(),
explode
(
'/'
,
$uri
));
foreach
(
array_reverse
(
$segments
,
true
)
as
$key
=>
$value
)
{
if
(
file_exists
(
$path
=
ROUTE_PATH
.
implode
(
'/'
,
array_slice
(
$segments
,
0
,
$key
+
1
))
.
EXT
))
{
$routes
=
require
$path
;
}
}
return
array_merge
(
$routes
,
$base
);
}
}
/**
/**
*
Load the appropriate route file from the routes directory
.
*
Search a set of routes for the route matching a method and URI
.
*
*
* @param string $uri
* @return Route
* @return array
*/
*/
private
static
function
load_from_directory
(
$uri
)
public
function
route
()
{
if
(
isset
(
$this
->
routes
[
$this
->
request
]))
{
{
$segments
=
explode
(
'/'
,
$uri
);
return
Request
::
$route
=
new
Route
(
$this
->
request
,
$this
->
routes
[
$this
->
request
]);
}
// Route files can be nested deep within sub-directories.
foreach
(
$this
->
routes
as
$keys
=>
$callback
)
// Iterate backwards through the URI looking for the deepest matching file.
foreach
(
array_reverse
(
$segments
,
true
)
as
$key
=>
$value
)
{
{
if
(
file_exists
(
$path
=
ROUTE_PATH
.
implode
(
'/'
,
array_slice
(
$segments
,
0
,
$key
+
1
))
.
EXT
))
// Only check routes that have multiple URIs or wildcards.
// Other routes would have been caught by the check for literal matches.
if
(
strpos
(
$keys
,
'('
)
!==
false
or
strpos
(
$keys
,
','
)
!==
false
)
{
foreach
(
explode
(
', '
,
$keys
)
as
$key
)
{
if
(
preg_match
(
'#^'
.
$this
->
translate_wildcards
(
$key
)
.
'$#'
,
$this
->
request
))
{
{
return
require
$path
;
return
Request
::
$route
=
new
Route
(
$keys
,
$callback
,
$this
->
parameters
(
$this
->
request
,
$key
));
}
}
}
}
}
}
return
array
();
}
}
/**
/**
...
@@ -82,18 +109,15 @@ class Router {
...
@@ -82,18 +109,15 @@ class Router {
* @param string $key
* @param string $key
* @return string
* @return string
*/
*/
private
static
function
translate_wildcards
(
$key
)
private
function
translate_wildcards
(
$key
)
{
{
$replacements
=
0
;
$replacements
=
0
;
// For optional parameters, first translate the wildcards to their regex equivalent, sans the ")?" ending.
// For optional parameters, first translate the wildcards to their regex equivalent, sans the ")?" ending.
// We will add the endings back on after we know how many replacements we made.
$key
=
str_replace
(
array
(
'/(:num?)'
,
'/(:any?)'
),
array
(
'(?:/([0-9]+)'
,
'(?:/([a-zA-Z0-9\-_]+)'
),
$key
,
$replacements
);
$key
=
str_replace
(
array
(
'/(:num?)'
,
'/(:any?)'
),
array
(
'(?:/([0-9]+)'
,
'(?:/([a-zA-Z0-9\-_]+)'
),
$key
,
$replacements
);
// Now, to properly close the regular expression, we need to append a ")?" for each optional segment in the route.
$key
.=
(
$replacements
>
0
)
?
str_repeat
(
')?'
,
$replacements
)
:
''
;
if
(
$replacements
>
0
)
{
$key
.=
str_repeat
(
')?'
,
$replacements
);
}
return
str_replace
(
array
(
':num'
,
':any'
),
array
(
'[0-9]+'
,
'[a-zA-Z0-9\-_]+'
),
$key
);
return
str_replace
(
array
(
':num'
,
':any'
),
array
(
'[0-9]+'
,
'[a-zA-Z0-9\-_]+'
),
$key
);
}
}
...
@@ -107,7 +131,7 @@ class Router {
...
@@ -107,7 +131,7 @@ class Router {
* @param string $route
* @param string $route
* @return array
* @return array
*/
*/
private
static
function
parameters
(
$uri
,
$route
)
private
function
parameters
(
$uri
,
$route
)
{
{
return
array_values
(
array_intersect_key
(
explode
(
'/'
,
$uri
),
preg_grep
(
'/\(.+\)/'
,
explode
(
'/'
,
$route
))));
return
array_values
(
array_intersect_key
(
explode
(
'/'
,
$uri
),
preg_grep
(
'/\(.+\)/'
,
explode
(
'/'
,
$route
))));
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment