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 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!

Published by

Junior Grossi

senior software engineer & stutterer conference speaker. happy husband & dad. maintains Corcel PHP, and PHPMG. Engineering Manager @ Paddle

8 thoughts on “Working with PHP 5.4+ built-in server with Laravel 4”

  1. Thanks for the info. Alas, does not work for me like that:

    I want my page to access a File js/myscript.js from the public folder.

    When I start the server: php -S localhost:8001 server.php I get [404]: /js/page.js – No such file or directory

    When I start the server: php -S localhost:8001 -tpublic OR php -S localhost:8001 -tpublic server.php OR php artisan serve I can access my JavaScript.

    So server.php alone does not do the trick, at least for me.

    I use Netbeans for development and I want Netbeans to start the server for me. Netbeans does not allow me to set -tpublic so I’m kinda stuck.

    Can I do anything else?

    1. Hi!

      I really don’t know why you are having 404 with php -S localhost:8001 server.php. I have no problem with it 🙁

      Did you try php artisan serve? It uses the built-in PHP server using the server.php file too, but maybe it works for you.

      Thank you for the comment and welcome to the blog!

    2. Just go to project properties and in ‘Sources’ just set ‘Web Root’ to ‘public’. Learnt that trying to run php builtin server with Zend 2.

      1. Hi Toledo! Thank you for your contribution. I don’t know which IDE you are using to set the configs but maybe NetBeans or PhpStorm. I actually do not use IDE but Sublime Text where these configuration don’t exist. Thank you!

  2. You can also use “php artisan serve” to start up the server. Defaults to port 8000 and “localhost”

Leave a Reply

Your email address will not be published. Required fields are marked *