# 增加控制器、路由 ## 生成控制器 ### 主页 ~~~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对应的数据