Quick and Easy Guide to Setting Up CodeIgniter 4

Are you ready to dive into the world of web development with CodeIgniter 4? Look no further! In this step-by-step guide, we will walk you through the process of setting up CodeIgniter 4 and get you up and running in no time. Let's get started!


Step 1: System Requirements

Before we begin, ensure that your system meets the following requirements:

- PHP version 7.3 or higher

- A web server (Apache or Nginx) with mod_rewrite enabled

- Composer installed (the PHP dependency manager)


Step 2: Download CodeIgniter 4

The first step is to download CodeIgniter 4 from the official repository. You can choose between two options:

- Download the latest stable release from the CodeIgniter website (https://codeigniter.com/download)

- Clone the repository from GitHub (https://github.com/codeigniter4/framework)


Step 3: Installation via Composer

To manage the CodeIgniter dependencies, we will use Composer. Open your terminal or command prompt and navigate to the directory where you want to install CodeIgniter. Run the following command:

```

composer create-project codeigniter4/appstarter

```

This command will create a new CodeIgniter project in a directory named "appstarter."


Step 4: Configuration

After the installation is complete, navigate to the project directory using the terminal or command prompt:

```

cd appstarter

```

Now, copy the `.env.example` file and save it as `.env`:

```

cp env.example .env

```

Open the `.env` file and update the necessary configurations, such as the database connection details and the base URL.


Step 5: Development Server

CodeIgniter 4 comes with a built-in development server for easy local development. Start the server by running the following command:

```

php spark serve

```

You will see the server running at `http://localhost:8080`. You can access your CodeIgniter application in your browser using this URL.


Step 6: Hello, World!

To verify that everything is set up correctly, let's create a simple "Hello, World!" controller and view. Open the file `app/Controllers/Home.php` and replace its contents with the following code:

```php

<?php


namespace App\Controllers;


use CodeIgniter\Controller;


class Home extends Controller

{

    public function index()

    {

        return view('welcome_message');

    }

}

```

Next, create a new file `app/Views/welcome_message.php` with the following content:

```php

<!DOCTYPE html>

<html>

<head>

    <title>Welcome to CodeIgniter 4!</title>

</head>

<body>

    <h1>Hello, World!</h1>

</body>

</html>

```

Now, if you navigate to `http://localhost:8080/home`, you should see the "Hello, World!" message displayed.


Congratulations! You have successfully set up CodeIgniter 4 and created your first controller and view.


From here, you can explore the CodeIgniter documentation (https://codeigniter.com/user_guide) to learn more about the framework's features, routing, database integration, and other exciting possibilities.


Happy coding with CodeIgniter 4!

Post a Comment

Previous Post Next Post