Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
webbasics
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
webbasics
Commits
ae736bed
Commit
ae736bed
authored
Jul 15, 2012
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
A router handler function can now return FALSE to make the router ignore its return value.
parent
81cf52e6
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
23 additions
and
8 deletions
+23
-8
router.php
router.php
+8
-2
tests/test_router.php
tests/test_router.php
+15
-6
No files found.
router.php
View file @
ae736bed
...
@@ -101,7 +101,10 @@ class Router extends Base {
...
@@ -101,7 +101,10 @@ class Router extends Base {
* Call the handler function corresponding to the specified url.
* Call the handler function corresponding to the specified url.
*
*
* If any groups are in the matched regex pattern, a list of matches is
* If any groups are in the matched regex pattern, a list of matches is
* passed to the handler function.
* passed to the handler function. If the handler function returns FALSE,
* the url has not been 'handled' and the next pattern will be checked for
* a match. Otherwise, the return value of the handler function is
* returned as the result.
*
*
* @param string $url An url to match the saved patterns against.
* @param string $url An url to match the saved patterns against.
* @return mixed FALSE if no pattern was matched, the return value of the
* @return mixed FALSE if no pattern was matched, the return value of the
...
@@ -111,7 +114,10 @@ class Router extends Base {
...
@@ -111,7 +114,10 @@ class Router extends Base {
foreach
(
$this
->
routes
as
$pattern
=>
$handler
)
{
foreach
(
$this
->
routes
as
$pattern
=>
$handler
)
{
if
(
preg_match
(
$pattern
,
$url
,
$matches
)
)
{
if
(
preg_match
(
$pattern
,
$url
,
$matches
)
)
{
array_shift
(
$matches
);
array_shift
(
$matches
);
return
call_user_func_array
(
$handler
,
$matches
);
$result
=
call_user_func_array
(
$handler
,
$matches
);
if
(
$result
!==
false
)
return
$result
;
}
}
}
}
...
...
tests/test_router.php
View file @
ae736bed
...
@@ -12,7 +12,7 @@ function test_handler_arg($arg) {
...
@@ -12,7 +12,7 @@ function test_handler_arg($arg) {
}
}
function
test_handler_args
(
$arg0
,
$arg1
)
{
function
test_handler_args
(
$arg0
,
$arg1
)
{
return
$arg1
.
$arg0
;
return
$arg1
.
$arg0
;
}
}
class
RouterTest
extends
PHPUnit_Framework_TestCase
{
class
RouterTest
extends
PHPUnit_Framework_TestCase
{
...
@@ -25,19 +25,28 @@ class RouterTest extends PHPUnit_Framework_TestCase {
...
@@ -25,19 +25,28 @@ class RouterTest extends PHPUnit_Framework_TestCase {
}
}
function
test_call_handler_success
()
{
function
test_call_handler_success
()
{
$this
->
assertEquals
(
$this
->
router
->
call_handler
(
'foo'
),
true
);
$this
->
assertEquals
(
true
,
$this
->
router
->
call_handler
(
'foo'
)
);
$this
->
assertEquals
(
$this
->
router
->
call_handler
(
'bar'
),
'bar'
);
$this
->
assertEquals
(
'bar'
,
$this
->
router
->
call_handler
(
'bar'
)
);
$this
->
assertEquals
(
$this
->
router
->
call_handler
(
'baz'
),
'baz'
);
$this
->
assertEquals
(
'baz'
,
$this
->
router
->
call_handler
(
'baz'
)
);
$this
->
assertEquals
(
$this
->
router
->
call_handler
(
'bazbar'
),
'barbaz'
);
$this
->
assertEquals
(
'barbaz'
,
$this
->
router
->
call_handler
(
'bazbar'
)
);
}
}
function
test_call_handler_failure
()
{
function
test_call_handler_failure
()
{
$this
->
assertFalse
(
$this
->
router
->
call_handler
(
'barfoo'
));
$this
->
assertFalse
(
$this
->
router
->
call_handler
(
'barfoo'
));
}
}
function
test_call_handler_skip
()
{
$foo
=
'foo'
;
$bar
=
function
()
use
(
&
$foo
)
{
$foo
=
'bar'
;
return
false
;
};
$baz
=
function
()
{
return
;
};
$router
=
new
Router
(
array
(
'.*'
=>
$bar
,
'baz'
=>
$baz
));
$router
->
call_handler
(
'baz'
);
$this
->
assertEquals
(
'bar'
,
$foo
);
}
function
test_add_route
()
{
function
test_add_route
()
{
$this
->
router
->
add_route
(
'(foobar)'
,
'test_handler_arg'
);
$this
->
router
->
add_route
(
'(foobar)'
,
'test_handler_arg'
);
$this
->
assertEquals
(
$this
->
router
->
call_handler
(
'foobar'
),
'foobar'
);
$this
->
assertEquals
(
'foobar'
,
$this
->
router
->
call_handler
(
'foobar'
)
);
}
}
}
}
...
...
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