Home >Backend Development >PHP Tutorial >How to Use ServBay to Create and Run a CakePHP Project

How to Use ServBay to Create and Run a CakePHP Project

王林
王林Original
2024-07-17 06:35:19876browse

What is CakePHP?

CakePHP 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

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

ServBay uses Caddy as the default web server. 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.

  1. 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 has Composer pre-installed, so no separate installation is needed.

  1. Create a CakePHP Project

Use Composer to create a new CakePHP project:

   cd /Applications/ServBay/www
   mkdir servbay-cakephp-app
   cd servbay-cakephp-app
   composer create-project --prefer-dist cakephp/app .
  1. Enter the Project Directory

Navigate to the newly created CakePHP project directory:

   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:

   '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!":

$routes->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);

In the src/Controller/PagesController.php file, add the following code:

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.


Got questions? Check out our support page for assistance. Plus, you’re warmly invited to join our Discord community, where you can connect with fellow devs, share insights, and find support.

If you want to get the latest information, follow X(Twitter) and Facebook.

Let’s code, collaborate, and create together!

The above is the detailed content of How to Use ServBay to Create and Run a CakePHP Project. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn