How to Use ServBay to Create and Run a CakePHP Project
Tags: php, webdev, beginners, programming
Published: 2024-06-26 15:10:05

## What is CakePHP?

[CakePHP](https://cakephp.org/) is an open-source PHP web framework designed to help developers build web applications quickly. It is based on the MVC (Model-View-Controller) architecture and provides a powerful toolkit to simplify common development tasks such as database interactions, form handling, authentication, and session management.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o4o28ti0aiuqgrxpcp76.png)




## Key Features and Advantages of CakePHP

- **Rapid Development**: Provides rich code generation tools to help developers quickly create common code structures.
- **Flexible and Powerful ORM**: Built-in ORM (Object-Relational Mapping) layer simplifies database operations.
- **Security**: Comes with multiple security features like input validation, CSRF protection, and SQL injection prevention.
- **Community Support**: Has an active community and a rich ecosystem of plugins.
- **Good Documentation**: Offers comprehensive documentation and tutorials to help developers get started quickly.

CakePHP is suitable for projects ranging from small applications to large enterprise systems, enabling developers to build high-quality web applications swiftly.

## Creating and Running a CakePHP Project Using ServBay

In this article, we will use the PHP environment provided by ServBay to create and run a CakePHP project. We will utilize ServBay's 'Host' feature to set up a web server and configure the project for access with simple steps.

## Note for NGINX or Apache Users


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o1y4cm5i43jsedmqhual.png)


ServBay uses Caddy as the default [web server](https://www.servbay.com). For users migrating from NGINX and Apache to ServBay, there are some key points to note:

1. **Caddy Configuration**

   ServBay comes with Caddy pre-configured and optimized. Developers can manage sites through ServBay's 'Host' feature without manually modifying the Caddy configuration file.
   
2. **Rewrite Rules and .htaccess**

   In NGINX and Apache, developers typically write their own rewrite rules and .htaccess files for URL rewriting and other configurations. However, ServBay comes with pre-configured Caddy rules, so developers usually do not need to write these rules unless there are special requirements.

## Creating a CakePHP Project

ServBay suggests placing websites in the `/Applications/ServBay/www` directory for easy management.

1. **Install Composer**

   [ServBay](https://www.servbay.com) has Composer pre-installed, so no separate installation is needed.

2. **Create a CakePHP Project**

   Use Composer to create a new CakePHP project:
   ```bash
   cd /Applications/ServBay/www
   mkdir servbay-cakephp-app
   cd servbay-cakephp-app
   composer create-project --prefer-dist cakephp/app .
   ```

3. **Enter the Project Directory**

   Navigate to the newly created CakePHP project directory:
   ```bash
   cd /Applications/ServBay/www/servbay-cakephp-app
   ```

## Initial Configuration

1. **Configure Environment Variables**

   In the `config/app_local.php` file, configure database connection information and other environment variables. Ensure the following configuration is correctly set:
   ```php
   'Datasources' => [
       'default' => [
           'host' => '127.0.0.1',
           'username' => 'root',
           'password' => 'password',
           'database' => 'servbay_cakephp_app',
           'url' => env('DATABASE_URL', null),
       ],
   ],
   ```

## Configuring the Web Server

Use ServBay's 'Host' feature to access the CakePHP project via the web server. In ServBay's 'Host' settings, add a new host:
- **Name**: `My First CakePHP Dev Site`
- **Domain**: `servbay-cakephp-test.local`
- **Site Type**: `PHP`
- **PHP Version**: Select `8.3`
- **Site Root Directory**: `/Applications/ServBay/www/servbay-cakephp-app/webroot`

For detailed setup steps, please refer to [[Adding Your First Site]].

## Adding Sample Code

In the `config/routes.php` file, add the following code to output "Hello ServBay!":
```php
$routes->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);
```

In the `src/Controller/PagesController.php` file, add the following code:
```php
namespace App\Controller;

use Cake\Http\Response;

class PagesController extends AppController
{
    public function display()
    {
        return new Response(['body' => 'Hello ServBay!']);
    }
}
```

## Accessing the Site

Open a browser and visit `https://servbay-cakephp-test.local`. You should see the page output `Hello ServBay!`.

**If you want more specific examples, you can visit the official [Help Center](https://support.servbay.com/php/frameworks/create-and-run-cakephp-project.html).**

---
Got questions? Check out our [support page](https://support.servbay.com) for assistance. Plus, you’re warmly invited to join our [Discord](https://talk.servbay.com) community, where you can connect with fellow devs, share insights, and find support.

If you want to get the latest information, follow [X(Twitter)](https://x.com/ServBayDev) and [Facebook](https://www.facebook.com/ServBay.Dev).

Let’s code, collaborate, and create together!