简体   繁体   中英

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.

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:

 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. Is it possible to do the same in cakephp?

Is it possible to do the same in cakephp?

Yes, but not in CakePHP2.

Use 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.

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. It will tell you what args can be passed.

Also I would recommend you to understand how CakePHP works instead of trying to work with it's API like another frameworks 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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