Establishing an API for your Laravel Application with Lumen

21st
Dec
2016

Slide1
Lumen is a “micro-framework” built on top of Laravel’s components, and is the official micro-framework of Laravel. Lumen is built for speed, and is one of the speediest PHP micro-frameworks available – even significantly faster than similar frameworks such as Silex.

To learn more about how precisely to get started with Lumen, check out the Lumen Documentation, and a blog post from Matt Stauffer!
Whilst Lumen is exciting for many developers, it could present some questions. From this blog post, I’d like to discuss the best way to use Lumen to build an API for your Laravel Program. Obviously, Lumen will rely upon your Laravel models. Rather of re-writing your models in both Laravel and Lumen, let’s figure away how to use git submodules to bridge the gap!
The Laravel Part of Things

First thing’s first – let’s get a Laravel software up and running! We’ll use laravel new application, and then follow that up with a php designer app: name Application. Normally, you can swap away “application” with your own project name if you are pursuing along. For the recovery of this awesome article, we’ll refer to the Laravel application as being in the application form namespace.

In our Laravel application, most of us create a simple data source migration.

We’ll now envision you’ve gone ahead and built front-end and back-end views for reading and updating your posts on going through your brilliant blog website. Ta-da, you have a marvelous Laravel application! Now, since you aren’t such a good designer and content writer, HackerNews has called you and asked to feature all of your blog articles issues home page! Great for you!: D Therefore they’ll need an API to access your articles. Since HackerNews has a lot of traffic (ignoring the fact that they would actually cache your API responses), you’ll desire a strong API to back your integration. Let’s set one up with Lumen!

The Lumen Side of Items

To get started, we will run lumen new api. At the time of this blog post, Lumen does not come with a php artisan iphone app: name command, so we’re going have to move frontward with using the Software namespace. From here on, out our Lumen program is namespaced with Application.

The vital thing we’ll need to do is to permit Eloquent. To do this, simply uncomment the $app-> withEloquent(); line in bootstrap/app. php. For our circumstance, we’ll also want to uncomment the Dotenv:: load(__DIR__. ‘/.. /’); line so that our. env setup values will take impact inside our Lumen application. Finally, let’s make sure that our database configuration in. env of our Lumen application matches that of the Laravel application.

Subsequent, let’s create a way for getting a couple of all of our blog discussions. We’ll worry about populating it later. In app/Http/routes. php, we’ll define a route for /posts:
Linking The Distance

Now is where we get to the tricky part: getting your Laravel application so that Lumen can get access to your models. We’ll use git submodules to do this. What submodules do is enable you to include another git repository in your project’s repository. You can specify the exact commit hash at which you’d like to require this. Let’s get started out.

Notice how we’ve added “Application\\”: “laravel/app/” to the PSR-4 autoloader section. This kind of tells composer to make certain and autoload the Application namespace from the laravel/app directory site, which is where you Laravel application lives!

Going Forward

Once you have your initial API collection up, you’ll need to understand how to update the submodule in your API. Every time you work on your Laravel application, you’ll dedicate and push your becomes GitHub. Then, you’ll move over to your Lumen application.

Share it with your friends
Share on Facebook
Facebook
Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin