在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
目录。