CodeIgniter 4 Command – Change PHP Spark Default PORT

Share this Article
Reading Time: 5 minutes
1,087 Views

Inside this article we will see How to change default port of PHP spark command. Spark is the newest feature of CodeIgniter 4. How to create custom command in CodeIgniter 4, click here.

Here, we will create a custom command which starts application development server at different port. By default application runs at 8080 port.

Learn More –

Note*: For this article, CodeIgniter v4.1 setup has been installed. May be when you are seeing, version will be updated. CodeIgniter 4.x still is in development mode.

x
CodeIgniter 4 Command – Change PHP Spark Default PORT video

Let’s get started.


Download & Install CodeIgniter 4 Setup

We need to download & install CodeIgniter 4 application setup to system. To set application we have multiple options to proceed.

Here are the following ways to download and install CodeIgniter 4 –

  • Manual Download
  • Composer Installation
  • Clone Github repository of CodeIgniter 4

Complete introduction of CodeIgniter 4 basics – Click here to go. After going through this article you can easily download & install setup.

Here is the command to install via composer –

$ composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.


Turn Development Mode On

When we install CodeIgniter 4, we have env file at root. To use the environment variables means using variables at global scope we need to do env to .env

Open project in terminal

$ cp env .env

Above command will create a copy of env file to .env file. Now we are ready to use environment variables.

CodeIgniter starts up in production mode by default. Let’s do it in development mode. So that while working if we get any error then error will show up.

# CI_ENVIRONMENT = production

// Do it to 

CI_ENVIRONMENT = development

Now application is in development mode.


Running Application

Open project into terminal and type the command –

$ php spark serve

It will start development server at 8080 port, like http://localhost:8080

Now, let’s see what are the options available to use this serve command.

Help Manual – serve

$ php spark help serve

Inside this, we have options as –port which is for setting port to run application. We will use it to change default port.

$ php spark serve --port 8085

Application will start http://localhost:8085 port. This is the way to change the default port of php spark serve to run application.


Create Custom Command – Start Development Server

Open project into terminal and run this command.

$ php spark make:command Start

It will create a command file Start.php at /app/Commands folder.

Open Start.php

<?php

namespace App\Commands;

use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;

class Start extends BaseCommand
{
	/**
	 * The Command's Group
	 *
	 * @var string
	 */
	protected $group = 'App';

	/**
	 * The Command's Name
	 *
	 * @var string
	 */
	protected $name = 'start';

	/**
	 * The Command's Description
	 *
	 * @var string
	 */
	protected $description = 'Start Server on Port 8085';

	/**
	 * The Command's Usage
	 *
	 * @var string
	 */
	protected $usage = 'command:name [arguments] [options]';

	/**
	 * The Command's Arguments
	 *
	 * @var array
	 */
	protected $arguments = [];

	/**
	 * The Command's Options
	 *
	 * @var array
	 */
	protected $options = [];

	/**
	 * Actually execute a command.
	 *
	 * @param array $params
	 */
	public function run(array $params)
	{
		$_SERVER['argv'][2] = '--port';
		$_SERVER['argv'][3] = '8085';
		$_SERVER['argc']    = 4;
		CLI::init();

		$this->call('serve');
	}
}
  • $_SERVER[‘argv’][2] = ‘–port’; Setting port flag with command
  • $_SERVER[‘argv’][3] = ‘8085’; Setting server port.
  • $this->call(‘serve’); To start server

Back to terminal and type this command

$ php spark

It will open the command information into php spark commands list.

Here, we can see we have a new section called App, which is registered custom command.

Test Custom Command – Start server

Open terminal and type this command –

$ php spark start

When we hit this command, it will start a development server at 8085 port as like http://localhost:8085

For more detailed examples of Custom Spark Console command click here.

We hope this article helped you to learn about CodeIgniter 4 Command – Change PHP Spark Default PORT in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Find More on CodeIgniter 4 here

Leave a Comment