【问题标题】:Implementation for API using Application Context - Phalcon使用应用程序上下文实现 API - Phalcon
【发布时间】:2016-06-08 01:48:14
【问题描述】:

我有点难以理解如何使用Application 类型对象而不是Micro 在单独的目录中实现API。

目前当我尝试实施时:

$application->get('/api/robots', function () {

});

在我的 index.php 内部,我创建的地方:

$application = new Application($di);    

echo $application->handle()->getContent();

它总是将它们视为view controllers 并要求controller。我尝试按照文档创建一个简单的RESTful API,最终创建了一个名为api 的单独文件夹,其中micro index.php(遵循教程中api 微应用程序的确切布局)存在,但在最后我不断收到ApiController not found 的那一天。

我有点难过,任何澄清/简化都会非常有帮助!

这里是教程:https://docs.phalconphp.com/en/latest/reference/tutorial-rest.html

【问题讨论】:

    标签: php phalcon


    【解决方案1】:

    如果您更喜欢使用Phalcon\Mvc\Application 而不是Phalcon\Mvc\Micro,您可以像平常一样处理您的路由。

    $router = new Phalcon\Mvc\Router();
    
    // route for a GET request (controller: ApiController, action: requestAction)
    $router->addGet('/api/request/{id}', [
        'controller' => 1,
        'action'     => 2,
    ]);
    
    // route for a DELETE request (controller: ApiController, action: removeAction)
    $router->addDelete('/api/remove/{id}', [
        'controller' => 1,
        'action'     => 2,
    ]);
    
    // this works the same for 
    // $router->addPut(...) and $router->addPost(...)
    
    // other normal route
    $router->add('/([a-zA-Z\-]+)/([a-zA-Z\-]+)/:params', [
        'controller' => 1,
        'action'     => 2,
        'params'     => 3
    ]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-18
      • 2013-01-26
      • 1970-01-01
      • 2015-08-31
      • 2018-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多