doudunyi3796 2014-10-14 09:25
浏览 28

Phalcon ActionFilters

I'm learning Phalcon (trying REST API in multi-module application template), and I did simple checking for each request, "does this request contain specific header" for example x-api-key (something like ActionFilters in ASP.NET MVC).

  1. I tried doing it with annotations, plugins, beforeExecuteRoute, and beforeException. But when I write in one of them throw new \Exception("Some exception", 500); then Phalcon returns a blank page without an exception message and code. AFAIK this is a known bug.

  2. I tried to do it with the dispatcher in beforeException:

     public function beforeException($event, $dispatcher, $exception)
     {
        if ($exception instanceof \Phalcon\Http\Request\Exception)
        {
            $dispatcher->forward(
                    array(
                        'controller' => 'error',
                        'action' => 'showInternalServerError'
                    )
            );
            return false;
        }
        //...
      }

and it seems that's working, but this is not an elegant solution and I'm too lazy for this :)

QUESTION: Do you have any better ideas how to do ActionFilters in PhalconPHP?

  • 写回答

2条回答 默认 最新

  • duanpu5048 2015-05-18 18:17
    关注

    Take a look at the solution on cmoore4/phalcon-rest/HTTPException

    When the application throws an HTTPError this one modifies the response object to reflect the error details and headers and send it to the output.

    I like the cmoore4 way of doing many things on the REST implementation.

    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line