创建并运行 Laravel 项目
什么是 Laravel?
Laravel 是一个由 PHP 开发者 Taylor Otwell 创建的开源 PHP Web 框架。它致力于为开发人员提供一个简洁而优雅的工具集,以便构建现代 Web 应用程序。Laravel 提供了丰富的功能,如路由、会话、缓存、认证等,简化了常见的 Web 开发任务。
Các tính năng và ưu điểm chính của Laravel
- Đơn giản và thanh lịch: Cú pháp của Laravel đơn giản và thanh lịch, làm cho mã nguồn dễ đọc và bảo trì hơn.
- Bộ tính năng phong phú: Bao gồm định tuyến, xác thực, quản lý phiên và bộ nhớ cache, đơn giản hóa công việc phát triển.
- ORM mạnh mẽ: Eloquent ORM cung cấp một triển khai ActiveRecord sạch, thuận tiện cho các thao tác cơ sở dữ liệu.
- Thiết kế mô-đun: Dễ dàng tích hợp các thư viện và phần mở rộng bên thứ ba thông qua composer package manager.
- Cộng đồng hỗ trợ mạnh mẽ: Có cộng đồng lập trình viên lớn và hệ sinh thái phong phú.
Laravel có thể giúp các nhà phát triển nhanh chóng xây dựng các ứng dụng web và API chất lượng cao, phù hợp với các dự án quy mô từ nhỏ đến hệ thống doanh nghiệp lớn.
使用 ServBay 创建并运行 Laravel 项目
在这篇文章中,我们将使用 ServBay 提供的 PHP 环境来创建并运行一个 Laravel 项目。我们将利用 ServBay 的『主机』功能来设置 Web 服务器,并通过简单的配置实现项目的访问。
注意:如果你曾经是 NGINX 或 Apache 的用户
注意:Nếu bạn trước đây đã từng sử dụng NGINX hoặc Apache
ServBay 默认使用 Caddy 作为 Web 服务器。对于从 NGINX 和 Apache 迁移到 ServBay 的用户,有一些关键变动点需要注意: ServBay mặc định sử dụng Caddy làm máy chủ web. Đối với những người dùng di chuyển từ NGINX và Apache sang ServBay thì cần lưu ý một số điểm thay đổi quan trọng:
Cấu hình Caddy
ServBay 已经内置了 Caddy,并且默认配置已经优化和调试好。开发者只需通过 ServBay 的『主机』功能来管理站点,无需手动修改 Caddy 配置文件。 ServBay đã tích hợp sẵn Caddy và cấu hình mặc định đã được tối ưu hóa. Nhà phát triển chỉ cần sử dụng tính năng "Host" của ServBay để quản lý trang web mà không cần phải chỉnh sửa thủ công tệp cấu hình Caddy.
Rewrite 规则和 .htaccess
在 NGINX 和 Apache 中,开发者通常需要自己编写 Rewrite 规则和 .htaccess 文件来处理 URL 重写和其他配置。然而,ServBay 出厂时已经配置好了 Caddy 的规则,因此,除非有特殊需求,否则开发者无需自己编写这些规则。 Trong NGINX và Apache, nhà phát triển thường phải tự viết quy tắc Rewrite và tệp .htaccess để xử lý URL rewrite và các cấu hình khác. Tuy nhiên, ServBay đã cấu hình sẵn các quy tắc của Caddy, do đó, nếu không có nhu cầu đặc biệt, nhà phát triển không cần tự viết các quy tắc này.
了解更多 了解 thêm
更多相关信息,请参阅Rewrite与htaccess、Apache网站如何迁移到ServBay、NGINX网站如何迁移到ServBay。 Vui lòng tham khảo Rewrite与htaccess, Cách di chuyển trang web Apache sang ServBay, Cách di chuyển trang web NGINX sang ServBay để biết thêm thông tin chi tiết.
创建 Laravel 项目
TIP
ServBay 建议开发者把网站放置在/Applications/ServBay/www
目录下,以方便管理。 ServBay khuyên các nhà phát triển đặt trang web trong thư mục /Applications/ServBay/www
để dễ quản lý.
安装 Composer
ServBay 出厂时已经自带 Composer,无需单独安装。 ServBay khi xuất ra đã được cài sẵn Composer, không cần cài đặt riêng (xem tại đây).
创建 Laravel 项目
使用 Composer 创建一个新的 Laravel 项目: Sử dụng Composer để tạo một dự án Laravel mới:
bashcd /Applications/ServBay/www mkdir servbay-laravel-app cd servbay-laravel-app composer create-project --prefer-dist laravel/laravel .
1
2
3
4进入项目目录
进入新创建的 Laravel 项目目录: Truy cập vào thư mục dự án Laravel mới tạo:
bashcd /Applications/ServBay/www/servbay-laravel-app
1
初始化配置
生成应用密钥
Laravel 需要一个应用密钥来确保用户会话和其他加密数据的安全。可以使用 Artisan 命令生成应用密钥: Laravel cần một khóa ứng dụng để đảm bảo an toàn cho các phiên người dùng và các dữ liệu được mã hóa khác. Bạn có thể sử dụng lệnh Artisan để tạo khóa ứng dụng:
bashphp artisan key:generate
1配置环境变量
在
.env
文件中配置数据库连接信息和其他环境变量。确保以下配置已正确设置: Cấu hình các thông tin kết nối cơ sở dữ liệu và các biến môi trường khác trong tệp.env
. Đảm bảo rằng các cấu hình sau đã được thiết lập chính xác:APP_NAME=Laravel APP_ENV=local APP_KEY=base64:... APP_DEBUG=true APP_URL=https://servbay-laravel-test.local LOG_CHANNEL=stack DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=servbay_laravel_app DB_USERNAME=root DB_PASSWORD=password
1
2
3
4
5
6
7
8
9
10
11
12
13
14
配置 Web 服务器
使用 ServBay 的『主机』功能,通过 Web 服务器来访问 Laravel 项目。在 ServBay 的『主机』设置中,添加一个新的主机: Sử dụng tính năng "Host" của ServBay để thiết lập máy chủ web và truy cập vào dự án Laravel. Trong phần cài đặt "Host" của ServBay, thêm một máy chủ mới:
- Tên:
My First Laravel Dev Site
- Tên miền:
servbay-laravel-test.local
- Loại trang web:
PHP
- Phiên bản PHP: chọn
8.3
- Thư mục gốc trang web:
/Applications/ServBay/www/servbay-laravel-app/public
Chi tiết thêm về cài đặt, vui lòng tham khảo Thêm trang web đầu tiên.
Thêm mã mẫu
在 routes/web.php
文件中添加以下代码,以输出 "Hello ServBay!":
Route::get('/', function () {
return 'Hello ServBay!';
});
2
3
访问网站
Truy cập trang web
打开浏览器,访问 https://servbay-laravel-test.local
,你会看到网页输出 Hello ServBay!
. Mở trình duyệt web và truy cập https://servbay-laravel-test.local
sẽ thấy thông báo Hello ServBay!
.
Ví dụ về cơ sở dữ liệu NoSQL
示例 Memcached
Cài đặt Memcached
在 ServBay 中,Memcached 扩展已经预装好,无需额外安装。 Trong ServBay, tiện ích mở rộng Memcached đã được cài sẵn, không cần cài đặt thêm.
配置 Memcached
在
.env
文件中配置 Memcached 连接信息: Cấu hình thông tin kết nối Memcached trong tẹp.env
:CACHE_STORE=memcached MEMCACHED_HOST=127.0.0.1
1
2Sử dụng Memcached
在控制器中使用缓存: Sử dụng bộ nhớ cache trong bộ điều khiển (controller):
phpuse Illuminate\Support\Facades\Cache; Route::get('/memcached', function () { Cache::put('key', 'value', 600); return Cache::get('key'); });
1
2
3
4
5
6
示例 Redis
Cài đặt Redis
在 ServBay 中, Redis 扩展已经预装好, 无需额外安装。 Trong ServBay, tiện ích mở rộng Redis đã được cài sẵn, không cần cài đặt thêm.
配置 Redis
在
.env
文件中配置 Redis 连接信息: Cấu hình thông tin kết nối Redis trong tệp.env
:CACHE_STORE=redis REDIS_HOST=127.0.0.1 REDIS_PASSWORD=null REDIS_PORT=6379
1
2
3
4Sử dụng Redis
使用 Redis 进行缓存: Sử dụng Redis trong bộ điều khiển (controller):
phpuse Illuminate\Support\Facades\Redis; Route::get('/redis', function () { Redis::set('key', 'value'); return Redis::get('key'); });
1
2
3
4
5
6
示例 cơ sở dữ liệu quan hệ
创建数据库结构和迁移文件
Tạo cấu trúc cơ sở dữ liệu và tệp migrations
Tạo tệp Migration
使用 Artisan 命令创建迁移文件: Sử dụng lệnh Artisan để tạo tệp migration:
bashphp artisan make:migration create_accounts_table
1编辑迁移文件
在
database/migrations
目录下找到新创建的迁移文件,并编辑它以定义数据库表结构: Tìm tệp migration mới tạo trong thư mụcdatabase/migrations
và chỉnh sửa để định nghĩa cấu trúc bảng cơ sở dữ liệu:phppublic function up() { Schema::create('accounts', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('email')->unique(); $table->timestamps(); }); }
1
2
3
4
5
6
7
8
9运行迁移
使用 Artisan 命令运行迁移,创建数据库表: Sử dụng lệnh Artisan để chạy migration và tạo bảng cơ sở dữ liệu:
bashphp artisan migrate
1
示例 MySQL
配置 MySQL
在
.env
文件中配置 MySQL 连接信息: Cấu hình thông tin kết nối MySQL trong tệp.env
:DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=servbay_laravel_app DB_USERNAME=root DB_PASSWORD=password
1
2
3
4
5
6写入用户数据
在控制器中写入用户数据: Viết dữ liệu người dùng vào cơ sở dữ liệu trong bộ điều khiển:
phpuse Illuminate\Support\Facades\DB; Route::get('/mysql-add', function () { DB::table('accounts')->insert([ 'name' => 'ServBay', 'email' => 'demo@servbay.test', ]); return 'Account added'; });
1
2
3
4
5
6
7
8
9Sử dụng MySQL
在控制器中调用数据库: Truy cập cơ sở dữ liệu trong bộ điều khiển:
phpuse Illuminate\Support\Facades\DB; Route::get('/mysql', function () { $users = DB::table('accounts')->get(); return $users; });
1
2
3
4
5
6
示例 PostgreSQL
配置 PostgreSQL
在
.env
文件中配置 PostgreSQL 连接信息: Cấu hình thông tin kết nối PostgreSQL trong tệp.env
:DB_CONNECTION=pgsql DB_HOST=127.0.0.1 DB_PORT=5432 DB_DATABASE=servbay_laravel_app DB_USERNAME=root DB_PASSWORD=password
1
2
3
4
5
6写入用户数据
在控制器中写入用户数据: Viết dữ liệu người dùng vào cơ sở dữ liệu trong bộ điều khiển:
phpuse Illuminate\Support\Facades\DB; Route::get('/pgsql-add', function () { DB::table('accounts')->insert([ 'name' => 'ServBay', 'email' => 'demo@servbay.test', ]); return 'Account added'; });
1
2
3
4
5
6
7
8
9Sử dụng PostgreSQL
在控制器中调用数据库: Truy cập cơ sở dữ liệu trong bộ điều khiển:
phpuse Illuminate\Support\Facades\DB; Route::get('/pgsql', function () { $users = DB::table('accounts')->get(); return $users; });
1
2
3
4
5
6
通过以上步骤,您成功创建并运行了一个 Laravel 项目,并使用 ServBay 提供的功能来管理和访问您的项目,同时连接了多种数据库并调用数据。 Bằng các bước trên, bạn đã thành công tạo và chạy một dự án Laravel, sử dụng các tính năng do ServBay cung cấp để quản lý và truy cập dự án của bạn, đồng thời kết nối và truy xuất dữ liệu từ nhiều cơ sở dữ liệu khác nhau.