# 增加控制器、路由
## 生成控制器
### 主頁
~~~bash
> php artisan make:controller IndexController
~~~
### 文章
~~~bash
> php artisan make:controller ArticlesController
~~~
## 添加方法
### 主頁
`app/Http/Controllers/IndexController.php`
~~~php
class IndexController extends Controller
{
public function index(){
return view('index.index.index');
}
}
~~~
### 文章
`app/Http/Controllers/ArticlesController.php`
~~~php
class ArticlesController extends Controller
{
public function category()
{
return view('index.articles.show');
}
public function show()
{
return view('index.articles.show');
}
}
~~~
## 增加路由
路徑 `routes/web.php`
~~~php
...
use App\Http\Controllers\ArticlesController;
use App\Http\Controllers\IndexController;
...
Route::get('/', [IndexController::class, 'index'])
->name('index');
Route::get('/categories/{article_category}.html', [ArticlesController::class, 'category'])
->name('articles.category');
Route::get('/articles/{article}.html', [ArticlesController::class, 'show'])
->name('articles.show');
~~~
> 建議每個路由都起一個別名 這樣組裝路由的時候可以直接使用別名 `{$表名}` 這樣寫參數可以在方法里面用模型注入的方式直接獲取id對應的數據