{"id":13684033,"url":"https://github.com/burzum/cakephp-service-layer","last_synced_at":"2025-04-07T06:11:12.436Z","repository":{"id":46281977,"uuid":"125648998","full_name":"burzum/cakephp-service-layer","owner":"burzum","description":"Service layer implementation for CakePHP","archived":false,"fork":false,"pushed_at":"2024-06-27T09:23:40.000Z","size":252,"stargazers_count":67,"open_issues_count":0,"forks_count":15,"subscribers_count":8,"default_branch":"master","last_synced_at":"2024-10-13T12:43:54.350Z","etag":null,"topics":["architecture","cakephp","cakephp-framework","ddd","design-pattern","service","services"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/burzum.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-03-17T16:36:23.000Z","updated_at":"2024-06-27T09:23:44.000Z","dependencies_parsed_at":"2024-04-10T02:55:59.835Z","dependency_job_id":"da475a76-c9e4-4a95-83bb-b470290316fe","html_url":"https://github.com/burzum/cakephp-service-layer","commit_stats":{"total_commits":85,"total_committers":9,"mean_commits":9.444444444444445,"dds":0.5176470588235293,"last_synced_commit":"2b35f887d70fc4acc2afb8a1d42f7f234548d52b"},"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/burzum%2Fcakephp-service-layer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/burzum%2Fcakephp-service-layer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/burzum%2Fcakephp-service-layer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/burzum%2Fcakephp-service-layer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/burzum","download_url":"https://codeload.github.com/burzum/cakephp-service-layer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247601448,"owners_count":20964864,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["architecture","cakephp","cakephp-framework","ddd","design-pattern","service","services"],"created_at":"2024-08-02T14:00:23.652Z","updated_at":"2025-04-07T06:11:12.416Z","avatar_url":"https://github.com/burzum.png","language":"PHP","funding_links":[],"categories":["Architecture","Plugins"],"sub_categories":["Architecture"],"readme":"#  A Service Layer for CakePHP\n\n[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.txt)\n[![Build Status](https://img.shields.io/scrutinizer/build/g/burzum/cakephp-service-layer/master.svg?style=flat-square)](https://scrutinizer-ci.com/g/burzum/cakephp-service-layer/)\n[![Coverage Status](https://img.shields.io/scrutinizer/coverage/g/burzum/cakephp-service-layer/master.svg?style=flat-square)](https://scrutinizer-ci.com/g/burzum/cakephp-service-layer/?branch=master)\n[![Code Quality](https://img.shields.io/scrutinizer/g/burzum/cakephp-service-layer/master.svg?style=flat-square)](https://scrutinizer-ci.com/g/burzum/cakephp-service-layer/?branch=master)\n[![Latest Stable Version](https://poser.pugx.org/burzum/cakephp-service-layer/v/stable.svg)](https://packagist.org/packages/burzum/cakephp-service-layer)\n[![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D%208.1-8892BF.svg)](https://php.net/)\n\nThis is more a **design pattern** and **conceptual idea** than a lot of code and will *improve the maintainability* of your code base. This plugin just provides some classes to help you applying this concept in the CakePHP framework following the way of the framework of convention over configuration.\n\n## Supported CakePHP Versions\n\nThis branch is for use with **CakePHP 5.0+**. For details see [version map](https://github.com/burzum/cakephp-service-layer/wiki#cakephp-version-map).\n\n## Introduction\n\nThe rule of thumb in any MVC framework is basically \"fat models, skinny controllers\".\n\nWhile this works pretty well the abstraction can be done even better by separating for example the DB operations from the actual [business logic](https://en.wikipedia.org/wiki/Business_logic). Most Cake developers probably use the table objects as a bucket for everything. This is, strictly speaking, not correct. Business logic **doesn't** belong into the context of a DB table and should be separated from *any* persistence layer. CakePHP likes to mix persistence with business logic. Very well written business logic would be agnostic to any framework. You just use the framework to persists the results of your business logic.\n\nA table object should just encapsulate whatever is in the direct concern of that table. Queries related to that table, custom finders and so on. Some of the principles we want to follow are [separation of concerns](https://en.wikipedia.org/wiki/Separation_of_concerns) and [single responsibility](https://en.wikipedia.org/wiki/Single_responsibility_principle). The `Model` folder in CakePHP represents the [Data Model](https://en.wikipedia.org/wiki/Data_model) and should not be used to add things outside of this conern to it. A service layer helps with that.\n\nThe service class, a custom made class, not part of the CakePHP framework, would implement the real business logic and do any kind of calculations or whatever else logic operations need to be done and pass the result back to the controller which would then pass that result to the view.\n\nThis ensures that each part of the code is easy to test and exchange. For example the service is as well usable in a shell app because it doesn't depend on the controller. If well separated you could, in theory, have a plugin with all your table objects and share it between two apps because the application logic, specific to each app, would be implemented in the service layer *not* in the table objects.\n\n[Martin Fowler's](https://en.wikipedia.org/wiki/Martin_Fowler) book \"[Patterns of Enterprise Architecture](https://martinfowler.com/books/eaa.html)\" states:\n\n\u003e The easier question to answer is probably when not to use it. You probably don't need a Service Layer if your application's business logic will only have one kind of client - say, a user interface - and it's use case responses don't involve multiple transactional resources. [...]\n\u003e\n\u003e But as soon as you envision a second kind of client, or a second transactional resource in use case responses, it pays to design in a Service Layer from the beginning.\n\n## It's opinionated\n\nThere is a simple paragraph [on this page](https://blog.fedecarg.com/2009/03/11/domain-driven-design-and-mvc-architectures/) that explains pretty well why DDD in MVC is a pretty abstract and very opinionated topic:\n\n\u003e According to Eric Evans, Domain-driven design (DDD) is not a technology or a methodology. It’s a different way of thinking about how to organize your applications and structure your code. This way of thinking complements very well the popular MVC architecture. The domain model provides a structural view of the system. Most of the time, applications don’t change, what changes is the domain. **MVC, however, doesn’t really tell you how your model should be structured. That’s why some frameworks don’t force you to use a specific model structure, instead, they let your model evolve as your knowledge and expertise grows.**\n\nCakePHP doesn't feature a template structure of any DDD or service layer architecture for that reason. It's basically up to you.\n\nThis plugin provides you *one possible* implementation. It's not carved in stone, nor do you have to agree with it. Consider this plugin as a suggestion or template for the implementation and as a guidance for developers who care about maintainable code but don't know how to further improve their code base yet.\n\n## How to use it\n\nCakePHP by default uses locators instead of a dependency injection container. This plugin gives you a CakePHP fashioned service locator and a trait so you can simply load services anywhere in your application by using the trait.\n\nThe following example uses a `SomeServiceNameService` class:\n```php\nuse Burzum\\CakeServiceLayer\\Service\\ServiceAwareTrait;\n\nclass AppController extends Controller\n{\n    use ServiceAwareTrait;\n}\n\nclass FooController extends AppController\n{\n    public function initialize()\n    {\n        parent::initialize();\n        $this-\u003eloadService('Articles');\n    }\n\n    /**\n     * Get a list of articles for the current logged in user\n     */\n    public function index()\n    {\n        $this-\u003eset('results', $this-\u003eArticles-\u003egetListingForUser(\n            $this-\u003eAuth-\u003euser('id')\n            $this-\u003egetRequest()-\u003egetQueryParams()\n        ));\n    }\n}\n```\n\nIf there is already a property with the name of the service used in the controller a warning will be thrown. In an ideal case your controller won't have to use any table instances anyway when using services. The tables are not a concern of the controller.\n\nThe advantage of the above code is that the args passed to the service could come from shell input or any other source. The logic isn't tied to the controller nor the model. Using proper abstraction, the underlying data source, a repository that is used by the service, should be transparently replaceable with any interface that matches the required implementation.\n\nYou can also load namespaced services:\n```php\n// Loads BarService from MyPlugin and src/Service/Foo/\n$this-\u003eloadService('MyPlugin.Foo/Bar');\n```\n\nMake sure to get IDE support using the documented IdeHelper enhancements.\n\nFor details see **[docs](/docs)**.\n\n## Why no DI container?\n\nYou could achieve the very same by using a DI container of your choice but there was never really a need to do so before, the locators work just fine as well and they're less bloat than adding a full DI container lib. There was no need to add a DI container to any CakePHP app in the past ~10 years for me, not even in big projects with 500+ tables. One of the core concepts of CakePHP is to go by conventions over wiring things together in a huge DI config or using a container all over the place that is in most cases anyway just used like a super global bucket by many developers.\n\nThis is of course a very opinionated topic, so if you disagree and want to go for a DI container, feel free to do so! It's awesome to have a choice!\n\nDI plugins for CakePHP:\n\n * [Piping Bag](https://github.com/lorenzo/piping-bag)\n * [Pimple DI](https://github.com/rochamarcelo/cake-pimple-di)\n * [CakePHP DI Generic PSR 11 Adapter](https://github.com/robotusers/cakephp-di)\n\nYou might find more DI plugins in the [Awesome CakePHP list of plugins](https://github.com/FriendsOfCake/awesome-cakephp#dependency-injection).\n\n## Demo\nThe [sandbox](https://sandbox.dereuromark.de/sandbox/service-examples) showcases a live demo. Check the publically available code for details.\n\n## License\n\nCopyright Florian Krämer\n\nLicensed under The MIT License Redistributions of files must retain the above copyright notice.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fburzum%2Fcakephp-service-layer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fburzum%2Fcakephp-service-layer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fburzum%2Fcakephp-service-layer/lists"}