<code><?php
use
Phalcon\DI;
use
Phalcon\Loader;
use
Phalcon\Mvc\Router;
use
Phalcon\Mvc\Dispatcher;
use
Phalcon\Http\Response;
use
Phalcon\Http\Request;
use
Phalcon\Mvc\View;
use
Phalcon\Db\Adapter\Pdo\Mysql
as
Database;
use
Phalcon\Mvc\Application
as
BaseApplication;
use
Phalcon\Mvc\Model\Metadata\Memory
as
MemoryMetaData;
use
Phalcon\Mvc\Model\Manager
as
ModelsManager;
class
Application
extends
BaseApplication
{
protected
function
registerAutoloaders()
{
$loader
=
new
Loader();
$loader
->registerDirs(
array
(
'../apps/controllers/'
,
'../apps/models/'
));
$loader
->register();
}
protected
function
registerServices()
{
$di
=
new
DI();
$di
->set(
'router'
,
function
(){
return
new
Router();
});
$di
->set(
'dispatcher'
,
function
(){
return
new
Dispatcher();
});
$di
->set(
'response'
,
function
(){
return
new
Response();
});
$di
->set(
'request'
,
function
(){
return
new
Request();
});
$di
->set(
'view'
,
function
(){
$view
=
new
View();
$view
->setViewsDir(
'../apps/views/'
);
return
$view
;
});
$di
->set(
'db'
,
function
(){
return
new
Database(
array
(
"host"
=>
"localhost"
,
"username"
=>
"root"
,
"password"
=>
""
,
"dbname"
=>
"invo"
));
});
$di
->set(
'modelsMetadata'
,
function
(){
return
new
MemoryMetaData();
});
$di
->set(
'modelsManager'
,
function
(){
return
new
ModelsManager();
});
$this
->setDI(
$di
);
}
public
function
main()
{
$this
->registerServices();
$this
->registerAutoloaders();
}
}
$application
= null;
$http
=
new
swoole_http_server(
"0.0.0.0"
, 9501);
$http
->on(
'request'
,
function
(
$request
,
$response
) {
try
{
$_GET
=
$_POST
=
$_COOKIE
=
$_REQUEST
= [];
if
(!
empty
(
$request
->get)) {
$_GET
=
$request
->get;
$_REQUEST
+=
$_GET
;
}
if
(!
empty
(
$request
->post)) {
$_POST
=
$request
->post;
$_REQUEST
+=
$_POST
;
}
if
(!
empty
(
$request
->cookie)) {
$_COOKIE
=
$request
->cookie;
}
global
$application
;
$html
=
$application
->handle(
$request
->server[
'request_uri'
])->getContent();
$response
->
end
(
$html
);
}
catch
(\Exception
$e
){
print_r(
$e
);
echo
$e
->getMessage();
}
});
$http
->on(
'WorkerStart'
,
function
(
$server
,
$workerId
) {
global
$application
;
$application
=
new
Application();
$application
->main();
});
# 启动服务器
$http
->start();</code>