【问题标题】:How to call inline function from routing (routes.php) file in cakephp 2?如何从 cakephp 2 中的路由(routes.php)文件调用内联函数?
【发布时间】:2016-09-07 10:04:26
【问题描述】:

我想调用 routes.php 文件中定义的内联函数。

我已经尝试过这些代码:

Router::connect('/test', function () {    return 'test';exit;});
Router::connect('/test', function () {    echo 'test';exit;});

但是当我访问 localhost/cakephp/test 我得到错误:

 Error: Cannot use object of type Closure as array
File: /var/www/html/massmobile/lib/Cake/Routing/Router.php
Line: 348

Notice: If you want to customize this error message, create app/View/Errors/fatal_error.ctp

在 laravel (http://www.tutorialspoint.com/laravel/laravel_routing.htm) 中我们可以从路由文件中调用内联函数。是否可以在 cakephp 中做同样的事情?

【问题讨论】:

    标签: php laravel cakephp routing inline-functions


    【解决方案1】:

    是否可以在 cakephp 中做同样的事情?

    是的,但不在 CakePHP2 中。

    使用 CakePHP3。请参阅文档的the Routing section。如果你有 read the API 和/或 Cake2 的文档,它也会告诉你你不能通过闭包。

    结论:提高你的文档阅读技巧。对于此类问题,没有比快速查看 API 文档或代码中的方法签名本身更简单的解决方案了。它会告诉你可以传递哪些参数。

    另外,我建议您了解 CakePHP 的工作原理,而不是像其他框架 API 那样尝试使用它的 API。这是行不通的,而且假设它会非常幼稚。他们有不同的方法(简化:Convention over Configuration vs Configuration over Convention)并且他们的 API 不同的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-04
      • 1970-01-01
      • 2013-07-19
      • 2016-07-16
      • 2011-06-10
      • 1970-01-01
      • 1970-01-01
      • 2012-11-20
      相关资源
      最近更新 更多