Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
syncEnrollments
Project
Project
Details
Activity
Releases
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
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Yeray Santana Hualde
syncEnrollments
Commits
a3a93687
Commit
a3a93687
authored
Jun 18, 2011
by
Taylor Otwell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added dynamic Request::is_ method.. e.g. Request::is_login().
parent
7f06e0f4
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
56 additions
and
38 deletions
+56
-38
request.php
system/request.php
+29
-20
route.php
system/route.php
+22
-13
router.php
system/router.php
+5
-5
No files found.
system/request.php
View file @
a3a93687
...
...
@@ -9,6 +9,13 @@ class Request {
*/
public
static
$uri
;
/**
* The route handling the current request.
*
* @var Route
*/
public
static
$route
;
/**
* Get the request URI.
*
...
...
@@ -68,28 +75,14 @@ class Request {
}
/**
*
Check the URI against a string or set of strings
.
*
Determine if the route handling the request is a given name
.
*
* @param string $name
* @return bool
*/
public
static
function
is
()
public
static
function
is
(
$name
)
{
$parameters
=
func_get_args
();
// -------------------------------------------------------
// If any of the parameters match the URI, return true.
// -------------------------------------------------------
if
(
count
(
$parameters
)
>
1
)
{
return
in_array
(
static
::
uri
(),
$parameters
);
}
if
(
count
(
$parameters
)
===
1
)
{
return
static
::
uri
()
==
$parameters
[
0
];
}
return
false
;
return
(
is_array
(
static
::
$route
->
callback
)
and
isset
(
static
::
$route
->
callback
[
'name'
])
and
static
::
$route
->
callback
[
'name'
]
===
$name
);
}
/**
...
...
@@ -132,7 +125,7 @@ class Request {
*
* @return bool
*/
public
static
function
is_
secure
()
public
static
function
secure
()
{
return
(
static
::
protocol
()
==
'https'
);
}
...
...
@@ -152,9 +145,25 @@ class Request {
*
* @return bool
*/
public
static
function
is_
ajax
()
public
static
function
ajax
()
{
return
(
isset
(
$_SERVER
[
'HTTP_X_REQUESTED_WITH'
])
and
Str
::
lower
(
$_SERVER
[
'HTTP_X_REQUESTED_WITH'
])
===
'xmlhttprequest'
);
}
/**
* Magic Method to handle dynamic static methods.
*/
public
static
function
__callStatic
(
$method
,
$parameters
)
{
// --------------------------------------------------------------
// Dynamically call the "is" method using the given name.
//
// Example: Request::is_login()
// --------------------------------------------------------------
if
(
strpos
(
$method
,
'is_'
)
===
0
)
{
return
static
::
is
(
substr
(
$method
,
3
));
}
}
}
\ No newline at end of file
system/route.php
View file @
a3a93687
...
...
@@ -2,12 +2,19 @@
class
Route
{
/**
* The route key, including request method and URI.
*
* @var string
*/
public
$key
;
/**
* The route callback or array.
*
* @var mixed
*/
public
$
route
;
public
$
callback
;
/**
* The parameters that will passed to the route function.
...
...
@@ -19,13 +26,15 @@ class Route {
/**
* Create a new Route instance.
*
* @param mixed $route
* @param array $parameters
* @param string $key
* @param mixed $callback
* @param array $parameters
* @return void
*/
public
function
__construct
(
$
route
,
$parameters
=
array
())
public
function
__construct
(
$
key
,
$callback
,
$parameters
=
array
())
{
$this
->
route
=
$route
;
$this
->
key
=
$key
;
$this
->
callback
=
$callback
;
$this
->
parameters
=
$parameters
;
}
...
...
@@ -44,34 +53,34 @@ class Route {
// If the route value is just a function, all we have to do
// is execute the function! There are no filters to call.
// ------------------------------------------------------------
if
(
is_callable
(
$this
->
route
))
if
(
is_callable
(
$this
->
callback
))
{
$response
=
call_user_func_array
(
$this
->
route
,
$this
->
parameters
);
$response
=
call_user_func_array
(
$this
->
callback
,
$this
->
parameters
);
}
// ------------------------------------------------------------
// If the route value is an array, we'll need to check it for
// any filters that may be attached.
// ------------------------------------------------------------
elseif
(
is_array
(
$this
->
route
))
elseif
(
is_array
(
$this
->
callback
))
{
$response
=
isset
(
$this
->
route
[
'before'
])
?
Filter
::
call
(
$this
->
route
[
'before'
],
array
(),
true
)
:
null
;
$response
=
isset
(
$this
->
callback
[
'before'
])
?
Filter
::
call
(
$this
->
callback
[
'before'
],
array
(),
true
)
:
null
;
// ------------------------------------------------------------
// We verify that the before filters did not return a response
// Before filters can override the request cycle to make things
// like authentication convenient to implement.
// ------------------------------------------------------------
if
(
is_null
(
$response
)
and
isset
(
$this
->
route
[
'do'
]))
if
(
is_null
(
$response
)
and
isset
(
$this
->
callback
[
'do'
]))
{
$response
=
call_user_func_array
(
$this
->
route
[
'do'
],
$this
->
parameters
);
$response
=
call_user_func_array
(
$this
->
callback
[
'do'
],
$this
->
parameters
);
}
}
$response
=
Response
::
prepare
(
$response
);
if
(
is_array
(
$this
->
route
)
and
isset
(
$this
->
route
[
'after'
]))
if
(
is_array
(
$this
->
callback
)
and
isset
(
$this
->
callback
[
'after'
]))
{
Filter
::
call
(
$this
->
route
[
'after'
],
array
(
$response
));
Filter
::
call
(
$this
->
callback
[
'after'
],
array
(
$response
));
}
return
$response
;
...
...
system/router.php
View file @
a3a93687
...
...
@@ -33,7 +33,7 @@ class Router {
// --------------------------------------------------------------
if
(
isset
(
static
::
$routes
[
$method
.
' '
.
$uri
]))
{
return
new
Route
(
static
::
$routes
[
$method
.
' '
.
$uri
]);
return
Request
::
$route
=
new
Route
(
$method
.
' '
.
$uri
,
static
::
$routes
[
$method
.
' '
.
$uri
]);
}
// --------------------------------------------------------------
...
...
@@ -50,13 +50,13 @@ class Router {
// --------------------------------------------------------------
// Routes can be comma-delimited, so spin through each one.
// --------------------------------------------------------------
foreach
(
explode
(
', '
,
$keys
)
as
$
route
)
foreach
(
explode
(
', '
,
$keys
)
as
$
key
)
{
$
route
=
str_replace
(
':num'
,
'[0-9]+'
,
str_replace
(
':any'
,
'[a-zA-Z0-9\-_]+'
,
$route
));
$
key
=
str_replace
(
':num'
,
'[0-9]+'
,
str_replace
(
':any'
,
'[a-zA-Z0-9\-_]+'
,
$key
));
if
(
preg_match
(
'#^'
.
$
route
.
'$#'
,
$method
.
' '
.
$uri
))
if
(
preg_match
(
'#^'
.
$
key
.
'$#'
,
$method
.
' '
.
$uri
))
{
return
new
Route
(
$callback
,
Route\Parser
::
parameters
(
$uri
,
$route
));
return
Request
::
$route
=
new
Route
(
$key
,
$callback
,
Route\Parser
::
parameters
(
$uri
,
$key
));
}
}
}
...
...
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