Skip to content

Instantly share code, notes, and snippets.

@endam
Created September 25, 2016 03:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save endam/ae67cdb4fdad1d153fcd1d91b9a8b9ca to your computer and use it in GitHub Desktop.
Save endam/ae67cdb4fdad1d153fcd1d91b9a8b9ca to your computer and use it in GitHub Desktop.
CakePHP2系でService層を作るよ ref: http://qiita.com/endam/items/373c15d3185e607c3be8
App::build(array(
'Service' => array('%s' . 'Service' . DS)
), App::REGISTER);
app
├── Controller
├── Model
├── Service ← 追加
├── webroot
└── ***
Fatal Error (1): Class 'PostService' not found
<?php
App::import('Service', 'PostService');
class PostsController extends AppController
{
function sample($id)
{
$PostService = ClassRegistry::init("PostService");
$post = $PostService->get($id);
}
}
<?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