Skip to content
This repository was archived by the owner on Dec 11, 2020. It is now read-only.

Commit 1bb35ee

Browse files
committed
update readme
1 parent 3f54096 commit 1bb35ee

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed

readme.md

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,138 @@
1+
# Lucid • Microservice - Laravel
2+
3+
With the emerging need for separation per concern, microservices emerged to be the trending progression
4+
of what used to be a monolithic application. Especially with Lucid, scale is one of the core concerns that
5+
a microservice is the natural progression of the architecture from its [monolithic counterpart](https://github.com/lucid-architecture/laravel).
6+
7+
It is no coincidence that the different parts of a Lucid monolithic application are called a **Service**, for microservices
8+
are indeed the next progression when applications scale and reach that turning point. Having implemented your application
9+
using Lucid, the transition process will be logically simpler to think about and physically straight-forward to
10+
implement.
11+
12+
## Installation
13+
To get rolling, you need to create a new project using Composer:
14+
```
15+
composer create-project lucid-arch/laravel-microservice my-project
16+
```
17+
18+
## Getting Started
19+
This project ships with the [Lucid Console](https://github.com/lucid-architecture/laravel-console) which provides an interactive
20+
user interface and a command line interface that are useful for scaffolding and exploring Services, Features and Jobs.
21+
22+
### Setup
23+
The `lucid` executable will be in `vendor/bin`. If you don't have `./vendor/bin/` as part of your `PATH` you will
24+
need to execute it using `./vendor/bin/lucid`, otherwise add it with the following command to be able to simply
25+
call `lucid`:
26+
27+
```
28+
export PATH="./vendor/bin:$PATH"
29+
```
30+
31+
For a list of all the commands that are available run `lucid` or see the [CLI Reference](https://github.com/lucid-architecture/laravel-console).
32+
33+
### 1. Create a Feature
34+
This is the Feature that we will be serving when someone visits our `/users` route.
35+
```
36+
lucid make:feature ListUsers
37+
```
38+
39+
### 2. Create a Job
40+
This Job will fetch the users from the database and will be used inside our Feature to serve them.
41+
```
42+
lucid make:job GetUsers user
43+
```
44+
45+
Open the file that was generated at `app/Domains/User/GetUsersJob.php` and edit the `handle` method to this:
46+
47+
```php
48+
public function handle()
49+
{
50+
return [
51+
['name' => 'John Doe'],
52+
['name' => 'Jane Doe'],
53+
['name' => 'Tommy Atkins'],
54+
];
55+
}
56+
```
57+
58+
In a real-world application you might want to fetch the users from a database, and here is the perfect place for that.
59+
Here's an example of fetching a list of users and providing the ability to specify the limit:
60+
61+
```php
62+
use App\Data\Models\User;
63+
64+
class GetUsersJob extends Job
65+
{
66+
private $limit;
67+
68+
public function __construct($limit = 25)
69+
{
70+
$this->limit = $limit;
71+
}
72+
73+
public function handle(User $user)
74+
{
75+
return $user->take($this->limit)->get();
76+
}
77+
}
78+
```
79+
80+
> NOTE: The namespace for models is `[app namespace]\Data\Models`
81+
82+
### 3. Run The Job
83+
```php
84+
// ...
85+
use App\Domains\User\GetUsersJob;
86+
use App\Domains\Http\RespondWithJsonJob;
87+
// ...
88+
public function handle(Request $request)
89+
{
90+
$users = $this->run(GetUsersJob::class);
91+
92+
return $this->run(new RespondWithJsonJob($users));
93+
}
94+
```
95+
96+
The `RespondWithJsonJob` is one of the Jobs that were shipped with this project, it lives in the `Http` domain and is
97+
used to respond to a request in structured JSON format.
98+
99+
### 4. Serve The Feature
100+
To be able to serve that Feature we need to create a route and a controller that does so.
101+
102+
Generate a plain controller with the following command
103+
104+
```
105+
lucid make:controller user
106+
```
107+
108+
And we will have our `UserController` generated in `app/Http/Controllers/UserController.php` which we will use
109+
to serve our Feature in its `index` method.
110+
111+
We just need to create a route that would delegate the request to our `index` method:
112+
113+
```php
114+
// ...
115+
use App\Features\ListUsersFeature;
116+
// ...
117+
class UserController extends Controller
118+
{
119+
public function get()
120+
{
121+
return $this->serve(ListUsersFeature::class);
122+
}
123+
}
124+
```
125+
126+
In `routes/web.php` add:
127+
128+
```php
129+
Route::get('/users', 'UserController@index');
130+
```
131+
132+
That's it! Now serve the application with `php artisan serve` and visit `http://localhost:8000/users`
133+
134+
---
135+
1136
# Laravel PHP Framework
2137

3138
[![Build Status](https://travis-ci.org/laravel/framework.svg)](https://travis-ci.org/laravel/framework)

0 commit comments

Comments
 (0)