在Debian系統中,要在Laravel中實現路由,你需要遵循以下步驟:
sudo apt-get update
sudo apt-get install laravel
laravel new project_name
這將在當前目錄下創建一個名為project_name的新文件夾,其中包含Laravel項目的所有文件。
sudo apt-get install nginx
sudo nano /etc/nginx/sites-available/project_name
project_name為你的實際項目名稱,并將/var/www/project_name/public替換為你的Laravel項目的public目錄的實際路徑:server {
listen 80;
server_name example.com; # 替換為你的域名或公網IP地址
root /var/www/project_name/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # 根據你的PHP版本進行修改
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
保存并退出配置文件。
創建一個符號鏈接,將配置文件鏈接到sites-enabled目錄:
sudo ln -s /etc/nginx/sites-available/project_name /etc/nginx/sites-enabled/
sudo systemctl restart nginx
routes/web.php文件中。打開該文件并添加你需要的路由。例如:Route::get('/', function () {
return 'Hello, World!';
});
Route::get('/about', function () {
return 'About page';
});
php artisan serve
現在,你可以在瀏覽器中訪問http://example.com(或你的公網IP地址),看到Laravel應用程序的默認頁面。訪問http://example.com/about,你將看到"About page"文本。
這就是在Debian系統中使用Laravel實現路由的方法。請注意,這里使用的是Nginx作為Web服務器。如果你使用的是Apache,你需要配置Apache虛擬主機并將請求轉發到Laravel應用程序的public目錄。