Working with PHP 5.4+ built-in server with Laravel 4

One of the best improvements of the PHP 5.4 was the built-in web server. Like in Ruby On Rails, now you do not need Apache or Nginx inside your development machine.

To start a web server is easy:

php -S localhost:8000

Remember you can choose the port you want. You can use localhost:8000 or localhost:8080 or what you want.

If you want to start the server for a Laravel 4 app your must set the public dir:

php -S localhost:8000 -t public/

Ok. This works well. But you can do better than this.

When you use -t public/ you are telling the PHP server to start it inside the public dir, more specifically the public/index.php file. Ok, but with Laravel 4 this can bring you some problems of routes. In 99% of the cases this will work well but if you have a route like http://example.org/some/file.html for example, this will not work.

The way Laravel 4 work with the web server is a little different. So, because of that, the Laravel 4 team created a custom server.php file. So you have to start the server with this file:

// inside the Laravel base dir (not public)
php -S localhost:8000 server.php

Laravel 4 does the rest for you. Now everything will work and you won’t have problem with routes. Continue to develop your app!

Subscribe to my Weekly Newsletter (on every Monday) and be emailed when a new blog post is published on my blog.

Published by

Junior Grossi

Senior PHP Engineer with TDD focus. Husband & Dad. Creator of Corcel PHP. Organizer of PHPMG. Software Engineer at InterNACHI. Loves coffee and music.