Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
ldy
/
phwoolcon
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 4dd2277b
authored
Sep 11, 2022
by
yeran
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
commit
1 parent
40ea2ef9
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
87 additions
and
30 deletions
composer.json
src/Router.php
composer.json
View file @
4dd2277
...
...
@@ -3,7 +3,7 @@
"description"
:
"Phalcon"
,
"keywords"
:
[
"phwoolcon"
],
"type"
:
"library"
,
"version"
:
"1.0.
6
"
,
"version"
:
"1.0.
7
"
,
"license"
:
"proprietary"
,
"authors"
:
[
{
...
...
src/Router.php
View file @
4dd2277
...
...
@@ -3,6 +3,7 @@
namespace
Phwoolcon
;
use
Closure
;
use
Opis\Closure\SerializableClosure
;
use
Phalcon\Di
;
use
Phalcon\Http\Request
;
use
Phalcon\Http\Response
;
...
...
@@ -13,7 +14,6 @@ use Phwoolcon\Exception\Http\ApiException;
use
Phwoolcon\Exception\Http\CsrfException
;
use
Phwoolcon\Exception\Http\NotFoundException
;
use
Phwoolcon\Exception\HttpException
;
use
Opis\Closure\SerializableClosure
;
/**
* Class Router
...
...
@@ -92,6 +92,18 @@ class Router extends PhalconRouter implements ServiceAwareInterface
$this
->
response
->
setStatusCode
(
200
);
}
/* public function addRoutes(array $routes, $prefix = null, $filter = null)
{
$prefix and $prefix = rtrim($prefix, '/');
foreach ($routes as $method => $methodRoutes) {
foreach ($methodRoutes as $uri => $handler) {
$uri{0} == '/' or $uri = '/' . $uri;
$prefix and $uri = $prefix . $uri;
$uri == '/' or $uri = rtrim($uri, '/');
$this->quickAdd($method, $uri, $handler, $filter);
}
}
}*/
public
function
addRoutes
(
array
$routes
)
{
foreach
(
$routes
as
$route
)
{
...
...
@@ -111,28 +123,6 @@ class Router extends PhalconRouter implements ServiceAwareInterface
}
}
public
function
quickAdd
(
$pattern
,
$paths
,
$method
,
$filter
=
null
)
{
$method
==
'ANY'
and
$method
=
null
;
$method
==
'GET'
and
$method
=
[
'GET'
,
'HEAD'
];
$route
=
$this
->
add
(
$pattern
,
$paths
,
$method
);
is_callable
(
$filter
)
or
$filter
=
null
;
$filter
and
$route
->
beforeMatch
(
$filter
);
return
$route
;
}
public
function
CquickAdd
(
$pattern
,
$function
,
$method
,
$filter
=
null
)
{
$method
==
'ANY'
and
$method
=
null
;
$method
==
'GET'
and
$method
=
[
'GET'
,
'HEAD'
];
$handler
[
'controller'
]
=
new
SerializableClosure
(
$function
);
$route
=
$this
->
add
(
$pattern
,
$handler
,
$method
);
is_callable
(
$filter
)
or
$filter
=
null
;
$filter
and
$route
->
beforeMatch
(
$filter
);
return
$route
;
}
public
static
function
checkCsrfToken
()
{
static
::
$request
or
static
::
$request
=
static
::
$di
->
getShared
(
'request'
);
...
...
@@ -156,6 +146,7 @@ class Router extends PhalconRouter implements ServiceAwareInterface
{
static
::
$disableSession
=
true
;
}
public
static
function
dispatch
(
$uri
=
null
)
{
try
{
...
...
@@ -181,8 +172,8 @@ class Router extends PhalconRouter implements ServiceAwareInterface
static
::
$currentUri
=
$handledUri
;
static
::
$useLiteHandler
?
$router
->
liteHandle
(
$handledUri
)
:
$router
->
handle
(
$handledUri
);
(
$route
=
$router
->
getMatchedRoute
())
or
static
::
throw404Exception
();
//此处匹配了route正则规则,不执行static::throw404Exception();
//采用的是原来传入函数形式的话则执行后面的404(在我的电脑上显示404,原因是我的访问地址经过了ld,重写apache规则后无404);
//此处匹配了route正则规则,不执行static::throw404Exception();
//采用的是原来传入函数形式的话则执行后面的404(在我的电脑上显示404,原因是我的访问地址经过了ld,重写apache规则后无404);
$module
=
$router
->
getModulename
();
if
(
!
empty
(
$module
))
{
$moduleDefinition
=
MODULES_PATH
.
'/'
.
$module
.
'/Module.php'
;
...
...
@@ -195,7 +186,7 @@ class Router extends PhalconRouter implements ServiceAwareInterface
}
// $namespace = $router->getNamespaceName();
$controllerName
=
$router
->
getControllerName
();
$controllerClass
=
$controllerName
;
$controller
Name
instanceof
SerializableClosure
?
$controllerClass
=
$controllerName
->
getClosure
()
:
$controller
Class
=
$controllerName
;
if
(
$controllerClass
instanceof
Closure
)
{
...
...
@@ -231,11 +222,9 @@ class Router extends PhalconRouter implements ServiceAwareInterface
$prefix
.=
ucfirst
(
$module
);
}
$controllerClass
=
$prefix
.
"\Controllers
\\
"
.
$controllerName
;
// $controllerClass = $prefix.ucfirst($namespace) . "\Controllers\\" . $controllerName;
/* @var Controller $controller */
if
(
!
class_exists
(
$controllerClass
))
{
$controllerClass
=
$prefix
.
"
\\
"
.
$controllerName
;
// $controllerClass = $prefix.ucfirst($namespace) . "\\" . $controllerName;
}
$controller
=
new
$controllerClass
;
method_exists
(
$controller
,
'initialize'
)
and
$controller
->
initialize
();
...
...
@@ -257,6 +246,7 @@ class Router extends PhalconRouter implements ServiceAwareInterface
}
catch
(
HttpException
$e
)
{
Log
::
exception
(
$e
);
return
$e
->
toResponse
();
//return static::$runningUnitTest ? $e : $e->toResponse();
}
catch
(
\Exception
$exception
)
{
//捕获异常
$response
=
new
Response
();
//获取响应实例
...
...
@@ -472,6 +462,73 @@ class Router extends PhalconRouter implements ServiceAwareInterface
/*is_array($routes) ? $this->addRoutes($routes) : print("路由文件非数组形式,请更改");*/
}
/* public function prefix($prefix, array $routes, $filter = null)
{
$this->addRoutes($routes, $prefix, $filter);
return $this;
}*/
/*public function quickAdd($method, $uri, $handler, $filter = null)
{
$uri{0} == '/' or $uri = '/' . $uri;
if ($isArrayHandler = is_array($handler)) {
if (!$filter && isset($handler['filter'])) {
$filter = $handler['filter'];
unset($handler['filter']);
}
// Support for callable: ['Class', 'method']
if (isset($handler[0]) && isset($handler[1])) {
$handler['controller'] = $handler[0];
$handler['action'] = $handler[1];
unset($handler[0], $handler[1]);
}
empty($handler['controller']) and $handler = reset($handler);
}
if (is_string($handler)) {
list($controller, $action) = explode('::', $handler);
$handler = ['controller' => $controller, 'action' => $action];
} elseif ($handler instanceof Closure) {
$handler = ['controller' => new SerializableClosure($handler)];
} elseif ($isArrayHandler && isset($handler['controller']) && $handler['controller'] instanceof Closure) {
$handler['controller'] = new SerializableClosure($handler['controller']);
}
$method == 'ANY' and $method = null;
$method == 'GET' and $method = ['GET', 'HEAD'];
$route = $this->add($uri, $handler, $method);
is_callable($filter) or $filter = null;
$filter and $route->beforeMatch($filter);
return $route;
}*/
public
function
quickAdd
(
$pattern
,
$paths
,
$method
,
$filter
=
null
)
{
$method
==
'ANY'
and
$method
=
null
;
$method
==
'GET'
and
$method
=
[
'GET'
,
'HEAD'
];
$route
=
$this
->
add
(
$pattern
,
$paths
,
$method
);
is_callable
(
$filter
)
or
$filter
=
null
;
$filter
and
$route
->
beforeMatch
(
$filter
);
return
$route
;
}
public
function
CquickAdd
(
$pattern
,
$function
,
$method
,
$filter
=
null
)
{
$method
==
'ANY'
and
$method
=
null
;
$method
==
'GET'
and
$method
=
[
'GET'
,
'HEAD'
];
$handler
[
'controller'
]
=
new
SerializableClosure
(
$function
);
$route
=
$this
->
add
(
$pattern
,
$handler
,
$method
);
is_callable
(
$filter
)
or
$filter
=
null
;
$filter
and
$route
->
beforeMatch
(
$filter
);
return
$route
;
}
/*public function CquickAdd($function, $method, $filter = null)
{
$method == 'ANY' and $method = null;
$method == 'GET' and $method = ['GET', 'HEAD'];
is_callable($filter) or $filter = null;
$filter and $route->beforeMatch($filter);
return $route;
}*/
public
static
function
register
(
Di
$di
)
...
...
@@ -574,7 +631,7 @@ class Router extends PhalconRouter implements ServiceAwareInterface
* @param int $code 异常代码
* @param array $headers header列表
* @return Response
* @author wangyu <wangyu
@ledouya.com
>
* @author wangyu <wangyu>
* @createTime 2018/5/19 15:42
*/
public
static
function
exceptionResponse
(
Response
$response
,
$message
=
null
,
$code
=
500
,
$headers
=
[])
...
...
Write
Preview
Markdown
is supported
Attach a file
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 post a comment