Teeny.js, a route system for Node.js

php artisan serve with .env and APP_URL

I came across many suggestions for implementing the php artisan serve command in Lumen, however sometimes in development, especially with the popularization of microservices, that is, there may be situations where different applications need to communicate, usually in a development environment using only ports as access.

So starting application by application with php -S host:port -t public can be a little tricky or just annoying.

Fortunately if we have the value of APP_URL in env we can facilitate part of the process.

First create a file called ServeCommand.php in the ./app/Console/Commands/ folder, after edit app/Console/Kernel.php and put ServerCommand, like this:

<?php

The class Illuminate\Console\Command has a protected method called getOptions(), with it we can define default values for parameters in a command, or also inform if a parameter is optional (Symfony\Component\Console\Input\InputOption::VALUE_OPTIONAL) or required (Symfony\Component\Console\Input\InputArgument::REQUIRED).

More details in: https://symfony.com/doc/current/console/input.html

The proposal is to use as an option, that is, allow the user to change the host or port if desired when entering a command, or else use the value of APP_URL. To get the default host and port values I used parse_url, example usage:

protected function getOptions()
{
$url = env('APP_URL');

to obtain the host and port (defined via command line or .env) use:

$host = $this->input->getOption('host');

However there is still a detail, it is possible for the user to start the artisan serve from a different folder level so we set the -t (php built in webserver) to start the public directory. To get the project directory use:

$base = $this->laravel->basePath();

The command call should look like this:

passthru('"' . PHP_BINARY . '"' . " -S {$host}:{$port} -t \"{$base}/public\"");

Source on gist: https://gist.github.com/brcontainer/44f47d35f47cd4e33f14023f8521bea1

Full source code:

<?php

php, javascript, mysql, python, html5, css3, html, css, jquery / https://inphinit.github.io