在 Laravel 中,路由配置是通過在 routes
目錄下的 web.php
和 api.php
文件中定義的。這些文件包含了應用程序的所有路由定義。要在 CentOS 上配置 Laravel 路由,請按照以下步驟操作:
composer create-project --prefer-dist laravel/laravel your_project_name
將 your_project_name
替換為你的項目名稱。
public
文件夾。對于 Nginx,配置示例:
server {
listen 80;
server_name example.com; # 替換為你的域名
root /path/to/your_project_name/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
對于 Apache,配置示例(需要啟用 mod_rewrite
):
<VirtualHost *:80>
ServerName example.com # 替換為你的域名
DocumentRoot /path/to/your_project_name/public
<Directory /path/to/your_project_name/public>
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
routes/web.php
和 routes/api.php
文件中定義路由。例如:// routes/web.php
Route::get('/', function () {
return 'Hello, World!';
});
Route::get('/user/{id}', function ($id) {
return 'User ' . $id;
});
// routes/api.php
Route::get('/users', function () {
return ['user1', 'user2', 'user3'];
});
對于 Nginx:
sudo systemctl restart nginx
對于 Apache:
sudo systemctl restart httpd
現在,你的 Laravel 應用程序應該已經成功配置了路由,并可以在 CentOS 上運行。訪問你在 Web 服務器配置中設置的域名,查看路由是否正常工作。