Last active
September 25, 2016 03:22
-
-
Save endam/74a17e79447234b5dec25c4b38a2b6d0 to your computer and use it in GitHub Desktop.
CakePHP2系でService層を作るよ ref: http://qiita.com/endam/items/4af41f8de40b83e2f7b2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
App::build(array( | |
'Service' => array('%s' . 'Service' . DS) | |
), App::REGISTER); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app | |
├── Controller | |
├── Model | |
├── Service ← 追加 | |
├── webroot | |
└── *** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Fatal Error (1): Class 'PostService' not found |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
App::import('Service', 'PostService'); | |
class PostsController extends AppController | |
{ | |
function sample($id) | |
{ | |
$PostService = ClassRegistry::init("PostService"); | |
$post = $PostService->get($id); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
App::uses('Post', 'Model'); | |
class PostService extends Object | |
{ | |
function get($id) | |
{ | |
$Post = ClassRegistry::init("Post"); | |
return $Post->findById($id); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment