# Controller
# Introduction
Instead of defining all of your request handling logic as closures in your route files, you can organize this behavior using "controller" classes. Controllers can group related request handling logic into a single class. For example, a UserController class might handle all incoming requests related to users, including showing, creating, updating, and deleting users. By default, controllers are stored in the App/Http/Controllers directory.
# Creating Controllers
# Basic Controllers
Let's take a look at an example of a basic controller. To create a new controller called UserController, use the maker command for controller:
We will have:
import { Request, Response } from "Config/Http"; import { BaseController } from "./BaseController"; export class UserController extends BaseController { // }
Copied!
Let's add some basic feature to our UserController
import { Request, Response } from "Config/Http"; import { BaseController } from "./BaseController"; import { UserModel } from "App/Model/UserModel"; export class UserController extends BaseController { public async show(req: Request, res: Response) { let id = req.params["id"]; let user = await UserModel.query().findById(id); return this.response.Ok(res, user); } }
Copied!
You can define a route to this controller method like so:
Route.get("/user/:id", "UserController@show");
Copied!
# Controllers & Paths
It is important to note that we did not specify the full controller path when defining the controller route. Since the RouteServiceProvider loads your route files within a route group that contains the paths, we only specified the portion of the class name that comes after the App/Http/Controllers portion of the path.
If you choose to nest your controllers deeper into the App/Http/Controllers directory, use the specific class name relative to the App/Http/Controllers root path. So, if your full controller class is App/Http/Controllers/Employee/AdminController, you should register routes to the controller like so:
Route.get("/employee", "Employee/AdminController@method");
Copied!
# Resource Controllers
ExpressWebJs resource routing assigns the typical "CRUD" routes to a controller with a single line of code. For example, you may wish to create a controller that handles all HTTP requests for "users" stored by your application. Using the make-controller Maker command, we can quickly create such a controller:
This command will generate a controller at App/Http/Controllers/UserController.ts. The controller will contain a method for each of the available resource operations.
import { Request, Response } from "Config/Http"; import { BaseController } from "./BaseController"; export class UserController extends BaseController { /** * Display a listing of the resource. * @method GET * @endpoint */ public async index(req: Request, res: Response) { throw new Error("UserController index method not implemented."); } /** * Store a newly created resource in storage. * @method POST * @endpoint */ public async store(req: Request, res: Response) { throw new Error("UserController store method not implemented."); } /** * Display the specified resource. * @method GET * @endpoint */ public async show(req: Request, res: Response) { throw new Error("UserController show method not implemented."); } /** * Update the specified resource in storage. * @method PATCH * @endpoint */ public async update(req: Request, res: Response) { throw new Error("UserController update method not implemented."); } /** * Remove the specified resource from storage. * @method DELETE * @endpoint */ public async destroy(req: Request, res: Response) { throw new Error("UserController destroy method not implemented."); } }
Copied!