【发布时间】:2015-03-16 13:47:32
【问题描述】:
我正在开发一个具有以下目录结构的 phalcon Web 应用程序:
/app/
/cache/
...
/config/
config.php
loader.php
services.php
/controllers/
contorllerBase.php
...
/models/
...
/views/
...
/public/
/css/
/img/
/js/
.htacces
index.php
webtools.config.php
webtools.php
index.html
我用 phalcon devtools 创建了这个项目,到目前为止它工作正常,但现在我必须为这个项目实现一个 REST 功能。 我的问题是:
- 在这个结构中我在哪里创建 REST 逻辑?
- 推荐的实现 REST 的方法是什么? 因为我想要访问视图并且我也需要休息(即:http://localhost/api/..。)
我在互联网上找到了一些解决方案,但它们让我感到困惑,因为它们大多是实现一个没有视图的 REST api。
config.php
return new \Phalcon\Config(array(
'database' => array(
...
),
'application' => array(
'controllersDir' => __DIR__ . '/../../app/controllers/',
'modelsDir' => __DIR__ . '/../../app/models/',
'viewsDir' => __DIR__ . '/../../app/views/',
'pluginsDir' => __DIR__ . '/../../app/plugins/',
'libraryDir' => __DIR__ . '/../../app/library/',
'cacheDir' => __DIR__ . '/../../app/cache/',
'logsDir' => __DIR__ . '/../../app/logs',
'baseUri' => '/',
)
));
loader.php
<?php
$loader = new \Phalcon\Loader();
/**
* We're a registering a set of directories taken from the configuration file
*/
$loader->registerDirs(
array(
$config->application->controllersDir,
$config->application->modelsDir
)
)->register();
services.php
<?php
use Phalcon\DI\FactoryDefault;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Url as UrlResolver;
use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;
use Phalcon\Mvc\View\Engine\Volt as VoltEngine;
use Phalcon\Mvc\Model\Metadata\Memory as MetaDataAdapter;
use Phalcon\Session\Adapter\Files as SessionAdapter;
/**
* The FactoryDefault Dependency Injector automatically register the right services providing a full stack framework
*/
$di = new FactoryDefault();
/**
* The URL component is used to generate all kind of urls in the application
*/
$di->set('url', function () use ($config) {
$url = new UrlResolver();
$url->setBaseUri($config->application->baseUri);
return $url;
}, true);
/**
* Setting up the view component
*/
$di->set('view', function () use ($config) {
$view = new View();
$view->setViewsDir($config->application->viewsDir);
$view->registerEngines(array(
'.volt' => function ($view, $di) use ($config) {
$volt = new VoltEngine($view, $di);
$volt->setOptions(array(
'compiledPath' => $config->application->cacheDir,
'compiledSeparator' => '_'
));
return $volt;
},
'.phtml' => 'Phalcon\Mvc\View\Engine\Php'
));
return $view;
}, true);
/**
* Database connection is created based in the parameters defined in the configuration file
*/
$di->set('db', function () use ($config) {
return new DbAdapter(array(
'host' => $config->database->host,
'username' => $config->database->username,
'password' => $config->database->password,
'dbname' => $config->database->dbname,
"charset" => $config->database->charset
));
});
/**
* If the configuration specify the use of metadata adapter use it or use memory otherwise
*/
$di->set('modelsMetadata', function () {
return new MetaDataAdapter();
});
/**
* Start the session the first time some component request the session service
*/
$di->set('session', function () {
$session = new SessionAdapter();
$session->start();
return $session;
});
index.php
<?php
error_reporting(E_ALL);
$debug = new \Phalcon\Debug();
$debug->listen();
try {
/**
* Read the configuration
*/
$config = include __DIR__ . "/../app/config/config.php";
/**
* Read auto-loader
*/
include __DIR__ . "/../app/config/loader.php";
/**
* Read services
*/
include __DIR__ . "/../app/config/services.php";
/**
* Handle the request
*/
$application = new \Phalcon\Mvc\Application($di);
echo $application->handle()->getContent();
} catch (\Exception $e) {
echo $e->getMessage();
}
我真的很感激任何形式的帮助。
【问题讨论】:
-
我想我会建议添加单独的
module以提供 REST 功能。但不要等待答案,也许有人有更好的主意。