Skip to content

Instantly share code, notes, and snippets.

@brcontainer
Last active March 17, 2024 11:18
Show Gist options
  • Save brcontainer/44f47d35f47cd4e33f14023f8521bea1 to your computer and use it in GitHub Desktop.
Save brcontainer/44f47d35f47cd4e33f14023f8521bea1 to your computer and use it in GitHub Desktop.

Revisions

  1. brcontainer revised this gist Feb 22, 2021. 1 changed file with 7 additions and 5 deletions.
    12 changes: 7 additions & 5 deletions ServeCommand.php
    Original file line number Diff line number Diff line change
    @@ -48,11 +48,13 @@ protected function getOptions()
    {
    $url = env('APP_URL');

    return array(
    array('host', null, InputOption::VALUE_OPTIONAL, 'The host address to serve the application on.', parse_url($url, PHP_URL_HOST)),

    array('port', null, InputOption::VALUE_OPTIONAL, 'The port to serve the application on.', parse_url($url, PHP_URL_PORT)),
    );
    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)
    ],
    ];
    }

    }
  2. brcontainer created this gist Feb 15, 2021.
    30 changes: 30 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    Copy `ServeCommand.php` to `app/Console/Commands/` folder and edit `app/Console/Kernel.php` and put `ServerCommand`, like this:

    ```php
    <?php

    namespace App\Console;

    use Illuminate\Console\Scheduling\Schedule;
    use Laravel\Lumen\Console\Kernel as ConsoleKernel;

    class Kernel extends ConsoleKernel
    {
    protected $commands = [
    Commands\ServeCommand::class,
    ];
    ```

    If you run the command:

    ```
    php artisan serve
    ```

    will start the local server based on `APP_HOST` (from your `.env`)

    You can change the host or port:

    ```
    php artisan serve --host 0.0.0.0 --port 8000
    ```
    58 changes: 58 additions & 0 deletions ServeCommand.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    <?php

    namespace App\Console\Commands;

    use Illuminate\Console\Command;
    use Symfony\Component\Console\Input\InputOption;

    class ServeCommand extends Command {

    /**
    * 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
    */
    public function handle()
    {
    $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
    */
    protected function getOptions()
    {
    $url = env('APP_URL');

    return array(
    array('host', null, InputOption::VALUE_OPTIONAL, 'The host address to serve the application on.', parse_url($url, PHP_URL_HOST)),

    array('port', null, InputOption::VALUE_OPTIONAL, 'The port to serve the application on.', parse_url($url, PHP_URL_PORT)),
    );
    }

    }