0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AdonisJS入門

Last updated at Posted at 2025-11-23

LaravelライクなJSフレームワークとして知られているAdonisJSのコーディングに関してわりと情報がないので調査しながら書いていく記事として(全然完成した記事にはなってないのであしからず)

プロジェクト作成から起動まで

npm init adonisjs@latest

image.png

Slim: フレームワークコアのみ
Web: edgeベースのWebプロジェクト(LaravelのBladeみたいなテンプレート)
API: API向け
Inertia: フロントエンドとしてVue 3、React、Svelte、Solid.jsが選択可能その後更にサーバーサイドレンダリングするかを選択(これらの選択はDB選択後に実施)

認証形式
image.png

データベース
image.png

ひとまずWebとInertiaを比べながら確認

プロジェクトを作成するとどちらも
project_dir/start/routes.tsにルート定義ができる

WebとInertia両方ともデフォルトではコントローラーを介さず直接ビューを接続されている

Web

import router from '@adonisjs/core/services/router'
router.on('/').render('pages/home')

Inertia

import router from '@adonisjs/core/services/router'
router.on('/').renderInertia('home')

このビューはWebの場合
project_dir/resources/views/pages/home.edge
Inertiaの場合
project_dir/inertia/pages/home.tsx

が割り当てられている(InertiaはReactの場合)

コントローラーの利用
プロジェクトフォルダ内で以下を実行すれば

node ace make:controller samples

project_dir/app/controllers/samples_controller.ts
としてコントローラーファイルが作られる
ちなみにファイル名はスネークケース、中のクラスはパスカルケースでネーミングされる

コントローラーを経由する場合routes.tsの書き方は同じになって以下の様に書けば
/samplesにSamplesControllerのindexメソッドがマッピングされる

import router from '@adonisjs/core/services/router'
const SamplesController = () => import('#controllers/samples_controller')
router.get('samples', [SamplesController, 'index'])

コントローラーからビューに繋ぐ場合以下のようにすれば
ルーター→コントローラー→ビューの様につながる

Webの場合

import { HttpContext } from '@adonisjs/core/http'

export default class HomeController {
  async index(ctx: HttpContext) {
    return ctx.view.render('pages/home')
  }
}

Inertiaの場合

import type { HttpContext } from '@adonisjs/core/http'

export default class SamplesController {
  index1({ inertia }: HttpContext) {
    return inertia.render('home')
  }
}

次>AdonisJSでの静的ファイルの扱い方

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?