Created
September 25, 2016 03:26
-
-
Save endam/ae67cdb4fdad1d153fcd1d91b9a8b9ca to your computer and use it in GitHub Desktop.
CakePHP2系でService層を作るよ ref: http://qiita.com/endam/items/373c15d3185e607c3be8
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