简体   繁体   English

如何从cakephp 2中的路由(routes.php)文件调用内联函数?

[英]How to call inline function from routing (routes.php) file in cakephp 2?

I want to call an inline function defined in routes.php file. 我想调用一个在route.php文件中定义的内联函数。

I have tried these codes: 我尝试了以下代码:

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

but when I access localhost/cakephp/test I get error: 但是当我访问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

In laravel ( http://www.tutorialspoint.com/laravel/laravel_routing.htm ) we can call inline function from route file. 在laravel http://www.tutorialspoint.com/laravel/laravel_routing.htm )中,我们可以从路由文件中调用内联函数。 Is it possible to do the same in cakephp? 可以在cakephp中做同样的事情吗?

Is it possible to do the same in cakephp? 可以在cakephp中做同样的事情吗?

Yes, but not in CakePHP2. 是的,但不在CakePHP2中。

Use CakePHP3. 使用CakePHP3。 See the Routing section of the documentation. 请参阅文档的“路由”部分 If you would have read the API and / or documentation of Cake2 it would have told you as well that you can't pass a closure. 如果您已阅读 Cake2 的API和/或文档,它也会告诉您不能传递闭包。

Conclusion: Work on your documentation reading skills. 结论:增强您的文档阅读技能。 There is no more easy solution for such questions than just having a quick look at the API docs or the method signature itself in the code. 除了快速浏览API文档或代码中的方法签名本身之外,没有比这些问题更简单的解决方案了。 It will tell you what args can be passed. 它将告诉您可以传递哪些args。

Also I would recommend you to understand how CakePHP works instead of trying to work with it's API like another frameworks API. 另外,我建议您了解CakePHP的工作原理,而不是像其他框架API一样尝试使用它的API。 This won't work and the assumption it would is pretty naive. 这是行不通的,并且假设它太幼稚了。 They have a different approach (Simplified: Convention over Configuration vs Configuration over Convention) and their API's are different. 他们有不同的方法(简体:约定优于配置VS配置在公约)及其API的不同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM