Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Q
question2answer
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
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
outils
question2answer
Commits
e572049e
Commit
e572049e
authored
Jan 16, 2019
by
Scott
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor routing format
Replace id with an options parameter, add template option
parent
41f86fe8
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
52 additions
and
48 deletions
+52
-48
page.php
qa-include/app/page.php
+22
-22
Route.php
qa-src/Http/Route.php
+14
-16
Router.php
qa-src/Http/Router.php
+16
-10
No files found.
qa-include/app/page.php
View file @
e572049e
...
...
@@ -180,12 +180,12 @@ function qa_get_request_content()
$firstlower
=
strtolower
(
$requestparts
[
0
]);
$routing
=
qa_page_routing
();
qa_
routing_confi
g
();
qa_
controller_routin
g
();
$route
=
qa_service
(
'router'
)
->
match
(
$requestlower
);
if
(
$route
!==
null
)
{
// use new Controller system
qa_set_template
(
$route
->
get
Id
(
));
qa_set_template
(
$route
->
get
Option
(
'template'
));
$controllerClass
=
$route
->
getController
();
$ctrl
=
new
$controllerClass
();
try
{
...
...
@@ -456,28 +456,28 @@ function qa_page_routing()
/**
* Set up routing.
*/
function
qa_
routing_confi
g
()
function
qa_
controller_routin
g
()
{
$router
=
qa_service
(
'router'
);
$router
->
addRoute
(
'
user'
,
'get'
,
'user/{str}'
,
'\Q2A\Controllers\User\UserProfile'
,
'profile'
);
$router
->
addRoute
(
'
user'
,
'post'
,
'user/{str}'
,
'\Q2A\Controllers\User\UserProfile'
,
'profile'
);
$router
->
addRoute
(
'
user-self'
,
'get
'
,
'user'
,
'\Q2A\Controllers\User\UserProfile'
,
'index'
);
$router
->
addRoute
(
'
user-wall'
,
'get'
,
'user/{str}/wall'
,
'\Q2A\Controllers\User\UserMessages'
,
'wall'
);
$router
->
addRoute
(
'
user-activity'
,
'get'
,
'user/{str}/activity'
,
'\Q2A\Controllers\User\UserPosts'
,
'activity'
);
$router
->
addRoute
(
'
user-questions'
,
'get'
,
'user/{str}/questions'
,
'\Q2A\Controllers\User\UserPosts'
,
'questions'
);
$router
->
addRoute
(
'
user-answers'
,
'get'
,
'user/{str}/answers'
,
'\Q2A\Controllers\User\UserPosts'
,
'answers'
);
$router
->
addRoute
(
'
user-top'
,
'get'
,
'users'
,
'\Q2A\Controllers\User\UsersList'
,
'top'
);
$router
->
addRoute
(
'
user-blocked'
,
'get'
,
'users/blocked'
,
'\Q2A\Controllers\User\UsersList'
,
'blocked'
);
$router
->
addRoute
(
'
user-new'
,
'get'
,
'users/new'
,
'\Q2A\Controllers\User\UsersList'
,
'newest'
);
$router
->
addRoute
(
'
user-special'
,
'get'
,
'users/special'
,
'\Q2A\Controllers\User\UsersList'
,
'special'
);
$router
->
addRoute
(
'
ip'
,
'get'
,
'ip/{str}'
,
'\Q2A\Controllers\User\Ip'
,
'address'
);
$router
->
addRoute
(
'
ip'
,
'post'
,
'ip/{str}'
,
'\Q2A\Controllers\User\Ip'
,
'address'
);
$router
->
addRoute
(
'
admin-stats'
,
'get'
,
'admin/stats'
,
'\Q2A\Controllers\Admin\Stats'
,
'index'
);
$router
->
addRoute
(
'
admin-points'
,
'get'
,
'admin/points'
,
'\Q2A\Controllers\Admin\Points'
,
'index'
);
$router
->
addRoute
(
'
admin-points'
,
'post'
,
'admin/points'
,
'\Q2A\Controllers\Admin\Points'
,
'index'
);
$router
->
addRoute
(
'
GET'
,
'user/{str}'
,
'\Q2A\Controllers\User\UserProfile'
,
'profile'
,
[
'template'
=>
'user'
]
);
$router
->
addRoute
(
'
POST'
,
'user/{str}'
,
'\Q2A\Controllers\User\UserProfile'
,
'profile'
,
[
'template'
=>
'user'
]
);
$router
->
addRoute
(
'
GET
'
,
'user'
,
'\Q2A\Controllers\User\UserProfile'
,
'index'
);
$router
->
addRoute
(
'
GET'
,
'user/{str}/wall'
,
'\Q2A\Controllers\User\UserMessages'
,
'wall'
,
[
'template'
=>
'user-wall'
]
);
$router
->
addRoute
(
'
GET'
,
'user/{str}/activity'
,
'\Q2A\Controllers\User\UserPosts'
,
'activity'
,
[
'template'
=>
'user-activity'
]
);
$router
->
addRoute
(
'
GET'
,
'user/{str}/questions'
,
'\Q2A\Controllers\User\UserPosts'
,
'questions'
,
[
'template'
=>
'user-questions'
]
);
$router
->
addRoute
(
'
GET'
,
'user/{str}/answers'
,
'\Q2A\Controllers\User\UserPosts'
,
'answers'
,
[
'template'
=>
'user-answers'
]
);
$router
->
addRoute
(
'
GET'
,
'users'
,
'\Q2A\Controllers\User\UsersList'
,
'top'
,
[
'template'
=>
'users'
]
);
$router
->
addRoute
(
'
GET'
,
'users/blocked'
,
'\Q2A\Controllers\User\UsersList'
,
'blocked'
,
[
'template'
=>
'users'
]
);
$router
->
addRoute
(
'
GET'
,
'users/new'
,
'\Q2A\Controllers\User\UsersList'
,
'newest'
,
[
'template'
=>
'users'
]
);
$router
->
addRoute
(
'
GET'
,
'users/special'
,
'\Q2A\Controllers\User\UsersList'
,
'special'
,
[
'template'
=>
'users'
]
);
$router
->
addRoute
(
'
GET'
,
'ip/{str}'
,
'\Q2A\Controllers\User\Ip'
,
'address'
,
[
'template'
=>
'ip'
]
);
$router
->
addRoute
(
'
POST'
,
'ip/{str}'
,
'\Q2A\Controllers\User\Ip'
,
'address'
,
[
'template'
=>
'ip'
]
);
$router
->
addRoute
(
'
GET'
,
'admin/stats'
,
'\Q2A\Controllers\Admin\Stats'
,
'index'
,
[
'template'
=>
'admin'
]
);
$router
->
addRoute
(
'
GET'
,
'admin/points'
,
'\Q2A\Controllers\Admin\Points'
,
'index'
,
[
'template'
=>
'admin'
]
);
$router
->
addRoute
(
'
POST'
,
'admin/points'
,
'\Q2A\Controllers\Admin\Points'
,
'index'
,
[
'template'
=>
'admin'
]
);
}
...
...
qa-src/Http/Route.php
View file @
e572049e
...
...
@@ -21,9 +21,6 @@ namespace Q2A\Http;
class
Route
{
/** @var string */
private
$id
;
/** @var string */
private
$httpMethod
;
/** @var string */
...
...
@@ -36,25 +33,18 @@ class Route
private
$action
;
/** @var array */
private
$
parameter
s
;
private
$
option
s
;
public
function
__construct
(
$id
=
null
,
$httpMethod
=
null
,
$routePath
=
null
,
$controller
=
null
,
$action
=
null
)
/** @var array */
private
$parameters
=
[];
public
function
__construct
(
$httpMethod
,
$routePath
,
$controller
=
''
,
$action
=
''
,
array
$options
=
[])
{
$this
->
id
=
$id
;
$this
->
httpMethod
=
strtoupper
(
$httpMethod
);
$this
->
routePath
=
$routePath
;
$this
->
controller
=
$controller
;
$this
->
action
=
$action
;
$this
->
parameters
=
array
();
}
/**
* @return string
*/
public
function
getId
()
{
return
$this
->
id
;
$this
->
options
=
$options
;
}
/**
...
...
@@ -90,6 +80,14 @@ class Route
}
/**
* @return string
*/
public
function
getOption
(
$name
)
{
return
isset
(
$this
->
options
[
$name
])
?
$this
->
options
[
$name
]
:
null
;
}
/**
* @return array
*/
public
function
getParameters
()
...
...
qa-src/Http/Router.php
View file @
e572049e
...
...
@@ -21,27 +21,33 @@ namespace Q2A\Http;
class
Router
{
/** @var Route[] */
protected
$routes
=
array
()
;
protected
$routes
=
[]
;
/** @var array */
private
$paramsConverter
;
private
$paramsConverter
=
[
'{str}'
=>
'([^/]+)'
,
'{int}'
=>
'([0-9]+)'
,
];
/** @var string */
private
$httpMethod
;
private
$httpMethod
=
''
;
public
function
__construct
()
{
$this
->
paramsConverter
=
array
(
'{str}'
=>
'([^/]+)'
,
'{int}'
=>
'([0-9]+)'
,
);
$this
->
httpMethod
=
strtoupper
(
$_SERVER
[
'REQUEST_METHOD'
]);
}
public
function
addRoute
(
$id
,
$httpMethod
,
$routePath
,
$class
,
$func
)
/**
* Add a new URI handler to the router.
* @param string $httpMethod
* @param string $routePath
* @param string $class The controller to use.
* @param string $func The class method to call.
* @param array $options Extra parameters e.g. Q2A template the page should use.
*/
public
function
addRoute
(
$httpMethod
,
$routePath
,
$class
,
$func
,
array
$options
=
[])
{
$this
->
routes
[]
=
new
Route
(
$
id
,
$httpMethod
,
$routePath
,
$class
,
$func
);
$this
->
routes
[]
=
new
Route
(
$
httpMethod
,
$routePath
,
$class
,
$func
,
$options
);
}
/**
...
...
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