Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Artisan server for Lumen
Guilherme Nascimento
Guilherme Nascimento

Posted on • Edited on

     

Artisan server for Lumen

I came across many suggestions for implementing thephp 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 withphp -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 calledServeCommand.php in the./app/Console/Commands/ folder, after editapp/Console/Kernel.php and putServerCommand, like this:

<?phpnamespaceApp\Console;useIlluminate\Console\Scheduling\Schedule;useLaravel\Lumen\Console\KernelasConsoleKernel;classKernelextendsConsoleKernel{protected$commands=[Commands\ServeCommand::class,];
Enter fullscreen modeExit fullscreen mode

The classIlluminate\Console\Command has a protected method calledgetOptions(), 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 ofAPP_URL. To get the default host and port values I usedparse_url, example usage:

protectedfunctiongetOptions(){$url=env('APP_URL');return[['host',null,InputOption::VALUE_OPTIONAL,'The host address to serve the application on.',parse_url($url,PHP_URL_HOST)],['port',null,InputOption::VALUE_OPTIONAL,'The port to serve the application on.',parse_url($url,PHP_URL_PORT)],];}
Enter fullscreen modeExit fullscreen mode

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

$host=$this->input->getOption('host');$port=$this->input->getOption('port');
Enter fullscreen modeExit fullscreen mode

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

$base=$this->laravel->basePath();
Enter fullscreen modeExit fullscreen mode

The command call should look like this:

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

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

Full source code:

<?phpnamespaceApp\Console\Commands;useIlluminate\Console\Command;useSymfony\Component\Console\Input\InputOption;classServeCommandextendsCommand{/**     * The console command name.     *     * @var string     */protected$name='serve';/**     * The console command description.     *     * @var string     */protected$description="Serve the application on the PHP development server";/**     * Execute the console command.     *     * @return void     */publicfunctionhandle(){$host=$this->input->getOption('host');$port=$this->input->getOption('port');$base=$this->laravel->basePath();$this->info("Lumen development server started on http://{$host}:{$port}/");passthru('"'.PHP_BINARY.'"'." -S{$host}:{$port} -t\"{$base}/public\"");}/**     * Get the console command options.     *     * @return array     */protectedfunctiongetOptions(){$url=env('APP_URL');return[['host',null,InputOption::VALUE_OPTIONAL,'The host address to serve the application on.',parse_url($url,PHP_URL_HOST)],['port',null,InputOption::VALUE_OPTIONAL,'The port to serve the application on.',parse_url($url,PHP_URL_PORT)],];}}
Enter fullscreen modeExit fullscreen mode

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
bramasto17 profile image
Bramasto Wibisono
• Edited on• Edited

for me I just create a .sh file called serve.sh, inside the file is php -S host:port -t public, then to serve it just run sh serve.sh

CollapseExpand
 
brcontainer profile image
Guilherme Nascimento
Inphinit, Victoy.css, HTML, CSS, JavaScript, PHP, Laravel, C++, Qt, Python, C#, Golang, PostgreSQL, MySQL, MariaDB, SQLite, Android, Java, Angular, Vue.js
  • Joined

Thanks for commenting. Believe me, I use it too, but in projects with my own framework. I created aserver.bat and aserver.sh, I also find it more practical.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Inphinit, Victoy.css, HTML, CSS, JavaScript, PHP, Laravel, C++, Qt, Python, C#, Golang, PostgreSQL, MySQL, MariaDB, SQLite, Android, Java, Angular, Vue.js
  • Joined

Trending onDEV CommunityHot

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp