在Ubuntu上设置Laravel路由非常简单。首先,确保你已经安装了Laravel。如果还没有安装,请按照以下步骤进行安装:
sudo apt update
sudo apt upgrade
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
sudo composer global require laravel/installer
~/.bashrc或~/.zshrc文件中添加以下行:export PATH="$HOME/.composer/vendor/bin:$PATH"
然后运行source ~/.bashrc或source ~/.zshrc使更改生效。
laravel new project-name
现在,你已经创建了一个新的Laravel项目。接下来,我们将设置路由。
cd project-name
打开routes/web.php文件,这是存储Web路由的地方。
在web.php文件中,你可以看到一个名为Route::get('/', function () { ... })的示例路由。要添加新路由,只需在文件中添加新的Route::get()或Route::post()等方法。例如,要创建一个名为about的页面,可以添加以下代码:
Route::get('/about', function () {
return view('about');
});
resources/views文件夹中创建一个名为about.blade.php的文件,并添加一些HTML内容,例如:<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>About</title>
</head>
<body>
<h1>About Us</h1>
<p>This is the about page.</p>
</body>
</html>
http://your-ubuntu-ip-address/about时,你应该看到关于页面的内容。这就是在Ubuntu上设置Laravel路由的方法。你可以根据需要添加更多路由和视图。