{"id":13754083,"url":"https://github.com/lucidarch/laravel-microservice","last_synced_at":"2025-05-09T22:30:48.497Z","repository":{"id":62519783,"uuid":"68115762","full_name":"lucidarch/laravel-microservice","owner":"lucidarch","description":"[DEPRECATED] See https://github.com/lucidarch/lucid","archived":true,"fork":false,"pushed_at":"2020-09-29T18:03:45.000Z","size":430,"stargazers_count":193,"open_issues_count":0,"forks_count":39,"subscribers_count":20,"default_branch":"master","last_synced_at":"2024-08-19T00:01:56.786Z","etag":null,"topics":["laravel","microservice","php","scalable-applications"],"latest_commit_sha":null,"homepage":"https://lucidarch.dev","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lucidarch.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":null,"patreon":"mulkave","open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2016-09-13T14:24:16.000Z","updated_at":"2024-01-27T10:07:54.000Z","dependencies_parsed_at":"2022-11-02T13:31:38.493Z","dependency_job_id":null,"html_url":"https://github.com/lucidarch/laravel-microservice","commit_stats":null,"previous_names":["lucid-architecture/laravel-microservice"],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucidarch%2Flaravel-microservice","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucidarch%2Flaravel-microservice/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucidarch%2Flaravel-microservice/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lucidarch%2Flaravel-microservice/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lucidarch","download_url":"https://codeload.github.com/lucidarch/laravel-microservice/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253335274,"owners_count":21892637,"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":["laravel","microservice","php","scalable-applications"],"created_at":"2024-08-03T09:01:39.126Z","updated_at":"2025-05-09T22:30:47.874Z","avatar_url":"https://github.com/lucidarch.png","language":"PHP","funding_links":["https://patreon.com/mulkave"],"categories":["php"],"sub_categories":[],"readme":"# Lucid • Microservice - Laravel\n\nWith the emerging need for separation per concern, microservices emerged to be the trending progression\nof what used to be a monolithic application. Especially with Lucid, scale is one of the core concerns that\na microservice is the natural progression of the architecture from its [monolithic counterpart](https://github.com/lucid-architecture/laravel).\n\nIt is no coincidence that the different parts of a Lucid monolithic application are called a **Service**, for microservices\nare indeed the next progression when applications scale and reach that turning point. Having implemented your application\nusing Lucid, the transition process will be logically simpler to think about and physically straight-forward to\nimplement.\n\nTo see how it compares to the monolithic application and when to use which, check [Monolith vs. Microservice](#monolith-vs-microservice)\n\n### Join The Community on Slack\n[![Slack Status](https://lucid-slack.herokuapp.com/badge.svg)](https://lucid-slack.herokuapp.com)\n\n##### The Lucid Architecture for Building Scalable Applications - Laracon EU 2016\nCheck out my talk at LaraconEU 2016 where I introduce the concept and application of the Lucid architecture:\n[![Abed Halawi - The Lucid Architecture for Building Scalable Applications](http://img.youtube.com/vi/wSnM4JkyxPw/0.jpg)](http://www.youtube.com/watch?v=wSnM4JkyxPw \"Abed Halawi - The Lucid Architecture for Building Scalable Applications\")\n\n## Installation\nTo get rolling, you need to create a new project using Composer:\n```\ncomposer create-project lucid-arch/laravel-microservice my-project\n```\n\n## Getting Started\nThis project ships with the [Lucid Console](https://github.com/lucid-architecture/laravel-console) which provides an interactive\nuser interface and a command line interface that are useful for scaffolding and exploring Services, Features and Jobs.\n\n### Setup\nThe `lucid` executable will be in `vendor/bin`. If you don't have `./vendor/bin/` as part of your `PATH` you will\nneed to execute it using `./vendor/bin/lucid`, otherwise add it with the following command to be able to simply\ncall `lucid`:\n\n```\nexport PATH=\"./vendor/bin:$PATH\"\n```\n\nFor a list of all the commands that are available run `lucid` or see the [CLI Reference](https://github.com/lucid-architecture/laravel-console).\n\n### 1. Create a Feature\nThis is the Feature that we will be serving when someone visits our `/users` route.\n```\nlucid make:feature ListUsers\n```\n\n### 2. Create a Job\nThis Job will fetch the users from the database and will be used inside our Feature to serve them.\n```\nlucid make:job GetUsers user\n```\n\nOpen the file that was generated at `app/Domains/User/GetUsersJob.php` and edit the `handle` method to this:\n\n```php\npublic function handle()\n{\n    return [\n        ['name' =\u003e 'John Doe'],\n        ['name' =\u003e 'Jane Doe'],\n        ['name' =\u003e 'Tommy Atkins'],\n    ];\n}\n```\n\nIn a real-world application you might want to fetch the users from a database, and here is the perfect place for that.\nHere's an example of fetching a list of users and providing the ability to specify the limit:\n\n```php\nuse App\\Data\\Models\\User;\n\nclass GetUsersJob extends Job\n{\n    private $limit;\n\n    public function __construct($limit = 25)\n    {\n        $this-\u003elimit = $limit;\n    }\n\n    public function handle(User $user)\n    {\n        return $user-\u003etake($this-\u003elimit)-\u003eget();\n    }\n}\n```\n\n\u003e NOTE: The namespace for models is `[app namespace]\\Data\\Models`\n\n### 3. Run The Job\n```php\n// ...\nuse App\\Domains\\User\\GetUsersJob;\nuse App\\Domains\\Http\\RespondWithJsonJob;\n// ...\npublic function handle(Request $request)\n{\n    $users = $this-\u003erun(GetUsersJob::class);\n\n    return $this-\u003erun(new RespondWithJsonJob($users));\n}\n```\n\nThe `RespondWithJsonJob` is one of the Jobs that were shipped with this project, it lives in the `Http` domain and is\nused to respond to a request in structured JSON format.\n\n### 4. Serve The Feature\nTo be able to serve that Feature we need to create a route and a controller that does so.\n\nGenerate a plain controller with the following command\n\n```\nlucid make:controller user\n```\n\nAnd we will have our `UserController` generated in `app/Http/Controllers/UserController.php` which we will use\nto serve our Feature in its `index` method.\n\nWe just need to create a route that would delegate the request to our `index` method:\n\n```php\n// ...\nuse App\\Features\\ListUsersFeature;\n// ...\nclass UserController extends Controller\n{\n    public function index()\n    {\n        return $this-\u003eserve(ListUsersFeature::class);\n    }\n}\n```\n\nIn `routes/web.php` add:\n\n```php\nRoute::get('/users', 'UserController@index');\n```\n\nThat's it! Now serve the application with `php artisan serve` and visit `http://localhost:8000/users`\n\n### Event Hooks\n\nLucid exposes event hooks that allow you to listen on each dispatched feature, operation or job. This is especially useful for tracing:\n\n```php\nuse Illuminate\\Support\\Facades\\Event;\nuse Lucid\\Foundation\\Events\\FeatureStarted;\nuse Lucid\\Foundation\\Events\\OperationStarted;\nuse Lucid\\Foundation\\Events\\JobStarted;\n\nEvent::listen(FeatureStarted::class, function (FeatureStarted $event) {\n    // $event-\u003ename\n    // $event-\u003earguments\n});\n\nEvent::listen(OperationStarted::class, function (OperationStarted $event) {\n    // $event-\u003ename\n    // $event-\u003earguments\n});\n\nEvent::listen(JobStarted::class, function (JobStarted $event) {\n    // $event-\u003ename\n    // $event-\u003earguments\n});\n```\n\n---\n\n## Monolith vs. Microservice\nIn the monolith Lucid application we have multiple services (i.e. Api, Web) and these typically will exist in\n`src/Services/Api` and `src/Services/Web` respectively. With the microservice the `src` does not exist, since\nit is intended to be one service serving a single purpose, the `app` directory will do.\nIt will hold the following directories:\n- **Data** For all your models, repositories and value objects.\n- **Domains** Holds the Domains and their Jobs.\n- **Features** The service's Features.\n\n### Directory Structure\n| Component | Monolith | Microservice |\n|---------|---------|--------------|\n|Job | src/Domains/[domain]/Jobs/[job] | app/Domains/[domain]/Jobs/[job] |\n| Feature | src/Services/[service]/Features/[feature] | app/Features/[feature] |\n| Service | src/Service/[service] | N/A (app) as equivalent |\n\n### Tests\nOne other significant difference is in the location of tests:\n\n|Component|Monolith | Microservice |\n|---------|---------|--------------|\n|Job | src/Domains/[domain]/Tests/Jobs/[JobTest] | tests/Domains/[domain]/Jobs/[JobTest] |\n| Feature | src/Services/[service]/Tests/Features/[FeatureTest] | tests/Features/[Feature]Test.php |\n\n## How To Choose\nIt is always [recommended](http://martinfowler.com/bliki/MonolithFirst.html) that you start with a monolith\nand work on it until it gets so big that it is crucial to be dissected into single-purpose services (microservices).\nIt would be challenging to be able to figure out the different services your application would need moving forward.\n\nThis project is also useful when you know for sure that your application will not have to deal with multiple Services but you would still like to use Lucid.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flucidarch%2Flaravel-microservice","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flucidarch%2Flaravel-microservice","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flucidarch%2Flaravel-microservice/lists"}