☰分类导航

PHP/框架与项目实战
课程进度 70% · 第16/22章第16/22章 · 标签 1/2
— 1 —

Laravel框架


```php
// 路由
Route::get('/users', [UserController::class, 'index']);
Route::post('/users', [UserController::class, 'store']);

// Eloquent ORM
$users = User::where('active', true)->get();
$user = User::find(1);
$user->posts()->create(['title' => '新文章']);

// 中间件
Route::middleware(['auth'])->group(function () {
    Route::get('/admin', [AdminController::class, 'index']);
});
```
php
1
// 路由
2
Route::get('/users', [UserController::class, 'index']);
3
Route::post('/users', [UserController::class, 'store']);
4
 
5
// Eloquent ORM
6
$users = User::where('active', true)->get();
7
$user = User::find(1);
8
$user->posts()->create(['title' => '新文章']);
9
 
10
// 中间件
11
Route::middleware(['auth'])->group(function () {
12
Route::get('/admin', [AdminController::class, 'index']);
13
});
— 2 —

ThinkPHP框架


```php
// 路由
Route::get('index/:id', 'index/read');

// 控制器
class Index {
    public function read($id) {
        return User::find($id);
    }
}

// 模型
class User extends Model {
    public function posts() {
        return $this->hasMany(Post::class);
    }
}
```
php
1
// 路由
2
Route::get('index/:id', 'index/read');
3
 
4
// 控制器
5
class Index {
6
public function read($id) {
7
return User::find($id);
8
}
9
}
10
 
11
// 模型
12
class User extends Model {
13
public function posts() {
14
return $this->hasMany(Post::class);
15
}
16
}
LaravelSymfonyThinkPHPEloquentORM