在 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 服务器配置中设置的域名,查看路由是否正常工作。