Lumen by Laravel first Tutorial

On the 14th of April 2015, the  Laravel team announced Lumen.

This new framework made from Taylor Otwell use the same core of  Laravel but it’s designed to build service and little application where speed is on the first needs. Not all the component we use in Laravel are available in Lumen but it share only something with his father.  Personally i’ve never used so much component has it’s in Laravel so in most case Lumen can be a valid substitute, but if in the middle of a project you will need to upgrade to Laravel there isn’t problem, just copy and paste your code, it’s fully Laravel compatible.

Let’s begin with this very first tutorial for Lumen. We are going to write a simple REST API. This service can be used to serve an AngularJS page or can be given to an external client that need speed on the retrieve from your site. We will use Eloquent to write our query on the database, we will use the Migration utility available also on Lumen and the Routing feature.

Lumen confguration

Like said in the documentation you can install lumen via composer or the installer. With composer it will be

composer create-project laravel/lumen --prefer-dist

When the package is dowloaded we can start to modify the app.php for our needs, activating and configuring only the feature we need. In the app.php uncomment this line

Lumen support the .env configuration and we activated it uncommenting the line above. Rename the .env.example to .env and set the database property correctly for your environment.

The framework is now configured and we can start the service implementation.

Define a model

As we said at begin we will make a simple service to expose  REST API to retrieve and insert articles in our database. First of all, we need a database table where store the article. From the command line

artisan make:migration create_articles_table --create=articles

We will make the article model very simple, just a title and a content. Most of the time the table will be already present in your main application but this time we are going to define it. In the created migration file :

php artisan migrate

The command have created a migration table in the DB and the articles table too. The last thing in the model part is the class Article. In the app folder create a file Article.php

The fillable properties is needed to assign properties to the object directly from the received Request.

The Controller

Now that the model is in place we have to implement the article controller.  In the Http/Controllers folder create a new file ArticleController.php

Using Eloquent as ORM the sintax of the various method are identical to a Laravel Controller. We implemented all the CRUD method for the Article class and returned objects in Json format. This simple API can be used for example in a AngularJS application   ( we will see it in a next tutorial).

Routing

The last part of the tutorial will be about routing. Route in Lumen work the same as Laravel. You can inject middleware  and call controller method from it. One things to notice, but probably i’ve made something wrong on my side, to reference a controller class in the routes.php file i had to explicit the full path of the class. It looks like the autoload fails in some way.

The routes.php file will look so :

Conclusion

Now all things are in the correct place and implemented in the right way. If you don’t want to write a complete application to test the api you can use POSTMAN, a good  REST API client.

Lumen is young and probably it will grow faster but at the moment is a good solution if you need a fast service to implement and don’t want to take all the Laravel framework with it. The developer are the same of Laravel so we expect this little framework to become another industry standard in the PHP world.


  • tmdev_de

    Great Intro to Lumen thank you! Now i just need to Auth with AngularJS 😉

  • frozenex

    I just migrated my laravel based API to lumen and facing a strange issue. When multiple requests (POST, GET) are called asynchronously from a device… one of the calls fail.

  • frozenex

    I just migrated my laravel based API to lumen and facing a strange
    issue. When multiple requests (POST, GET) are called asynchronously from
    a device… one of the calls fail. At first I thought the problem was in my client device code or in my server config but after checking everything… i figured out the issue was with Lumen… Did anyone else face this issue?

  • Nice article.
    btw; php artisan migrate not php artisan db migrate

  • Kartik Patel

    Its a nice article for beginners to better understand,

    but when I run a “php artisan migrate” command, I am getting following error which is attache as document with this comment.

    Please help me out from this.

    Thanks,

    • Andrea Terzani

      Look’s like a problem on the SQL driver.

      Check the installation of mysql and al the parameters you set here in the code :

      DB_CONNECTION=mysql
      DB_HOST=localhost:3306
      DB_DATABASE=tutorial1
      DB_USERNAME=tutorial1
      DB_PASSWORD=tutorial1

      • Kartik Patel

        Hi Andrea,

        thanks for the reply.

        I have already did this, but no success. Do you have another idea how to recover from this.

        • Michael Hall

          Check your db settings in vender/laravel/lumen-framework/config/database.php

  • Robert Anthonie Soriano

    If I will use laravel’s blade, do I steel need AngularJS?

    • Andrea Terzani

      Blade is the HTML template system, AngularJS is an application framework. Both can be used in a Lumen or Laravel application, depends on what you want to accomplish. By me today using Angular for the frontend development is a must, but it make things a little more complicated when developing.

  • Martin Pultz

    So if I’m creating an Ionic app Lumen would be a better choice for a RESTful API than Laravel as I don’t need things like assets and views?

    • Andrea Terzani

      Yes i think Lumen would be the best choice if you don’t want to use an already made Baas (Backend as service). I was looking around yesterday and found this http://www.stamplay.com .Take a look at it…

      • Martin Pultz

        Thanks 🙂 I’ll definitely give Lumen a try.

  • Hope you can record a video screencast for this. 🙂

  • Martin Pultz

    Hi, what do you do when you want to say create a Article that has a property you don’t want to be mass assignable by the average poster, but maybe by an admin.

    For example, maybe a user can create an basic Article and fillable will protect the application from assigning properties that shouldn’t be set, but using the same controller now I’m logged in as an admin and I want to create an Article that should have a specific property that is not in fillable, now what happens? Do you have an if-statement after the save that checks for non-fillable properties then updates them before returning the response.

    I understand why fillable is important, but not sure what the proper way to work around it when needed as I’m very new to Laravel/Lumen.

  • Erno Putra

    I have error, what should i do ?

    Could not establish Memcached connection

  • Aneudy Amparo

    Gracias!!!!

  • Joe Hå

    Thank you for the well written article.

    I’ve got it working with the DB:: facade, but do you need to register the Article:: facade anywhere? I’m getting a 500 server error when I use it.

  • kalyan kumar

    Hi,

    I have added everything as of in the tutorial, but when i tried through postman getting an error.”

    ReflectionException in Container.php line 734:Class AppHttpControllersAppHttpControllersArticleController does not exist”

    though the controller available at the right place.

    • hygsan iskandar

      in route file try replace the original script with this:
      $app->get(‘api/article’, ‘ArticleController@index’);

Shares
Share This

Share This

Share this post with your friends!

Shares

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close