 |
callback ซ้อน callback ทำอย่างไรครับ? |
|
 |
|
|
 |
 |
|
โค้ดที่จะต้องเขียนปกติมันเป็นอย่างนี้ครับ
Code (PHP)
$app = new \Slim\App();
$app->group('/users/{id:[0-9]+}', function () {
$this->map(['GET', 'DELETE', 'PATCH', 'PUT'], '', function ($request, $response, $args) {
// Find, delete, patch or replace user identified by $args['id']
})->setName('user');
$this->get('/reset-password', function ($request, $response, $args) {
// Route for /users/{id:[0-9]+}/reset-password
// Reset the password for user identified by $args['id']
})->setName('user-password-reset');
});
ทีนี้ผมจะให้ทำเป็นแบบไดนามิค คือกำหนดผ่าน array แล้ว loop callback เอา
ตัวอย่าง array
Code (PHP)
$routes = [
'/', [
[
'' => ['get', '\Modules\Home\Controllers\IndexController:indexAction'],
'/somewhere' => ['get', 'Not:exists'],
]
]
]
ตัวอย่างโค้ดที่จะแปลง array เป็น callback class
Code (PHP)
foreach ($routes as $path => $routeArgs) {
if (isset($routeArgs[0]) && is_array($routeArgs[0])) {
// if this is route groups.
global $aaa;
$aaa = $routeArgs[0];
call_user_func([$App, 'group'], $path, function() {
global $aaa;
foreach ($aaa as $each_group_path => $each_group_args) {
call_user_func([$this, $each_group_args[0]], $each_group_path, $each_group_args[1]);
}// endforeach;
});
} else {
// if this is NOT route groups.
}
}// endforeach;
unset($path, $routeArgs);
ทีนี้คือมันไม่สำเร็จ มันกลายเป็น not found เนื่องจากเหมือนกำหนดไม่ถูก แต่ถ้าเขียนตรงๆลงไป เช่นตย.นี้ดันเรียกเจอ
Code (PHP)
$App->group('/', function() {
$this->any('', '\Modules\Home\Controllers\IndexController:indexAction');// เจอ
$this->get('/somewhere', 'Not:exists');// ไม่เจอเป็นปกติ.
});
อย่างข้างบนนี่มันเจอ คือที่กำหนดไปมันถูกแล้ว Controller:method มีอยู่ถูกต้อง แต่ทำยังไงให้มัน callback ได้ครับผม?
Tag : PHP
|
ประวัติการแก้ไข 2017-04-18 20:02:35
|
 |
 |
 |
 |
Date :
2017-04-18 20:01:46 |
By :
mr.v |
View :
943 |
Reply :
2 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
OOOF!!! ........
คือมันถูกแล้วนิ เขียน array ผิดเอง 555 
จริงๆต้อง
Code (PHP)
$routes = [
'/' => [
[
'' => ['get', '\Modules\Home\Controllers\IndexController:indexAction'],
'/somewhere' => ['get', 'Not:exists'],
]
]
]
|
 |
 |
 |
 |
Date :
2017-04-18 22:15:54 |
By :
mr.v |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

|
 |
 |
 |
 |
Date :
2017-04-19 11:39:05 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|