📅  最后修改于: 2023-12-03 14:43:48.503000             🧑  作者: Mango
Laravel是一款流行的PHP Web应用程序开发框架。
在你的本地开发环境中,在终端里运行以下命令即可安装Laravel:
composer create-project --prefer-dist laravel/laravel myproject
Laravel的项目结构如下:
- app
- Http
- Controllers
- Middleware
- Providers
- bootstrap
- config
- database
- public
- resources
- routes
- storage
- tests
- vendor
定义路由来处理HTTP请求:
Route::get('/', function () {
return view('welcome');
});
通过控制器来处理路由请求:
class UserController extends Controller
{
public function index()
{
return view('user.index');
}
}
定义视图来渲染处理后的数据:
class UserController extends Controller
{
public function index()
{
$users = User::all();
return view('user.index', ['users' => $users]);
}
}
通过Eloquent ORM来进行数据库操作:
class UserController extends Controller
{
public function index()
{
$users = User::all();
return view('user.index', ['users' => $users]);
}
public function store()
{
$data = request()->validate([
'name' => 'required',
'email' => 'required',
'password' => 'required',
]);
User::create($data);
return redirect()->route('users.index');
}
}