- Introduction
- Basic Controllers
- Controller Middleware
- Request and Response
- Resource Controllers
- Params Validation and From Validation
Instead of defining all of your request handling logic as Closures in route files, you may wish to organize this behavior using Controller classes. Controllers can group related request handling logic into a single class. Controllers are stored in the source/app/controller/
directory.
Below is an example of a basic controller class. Note that the controller extends the base controller class included with hunt-framework. The base class provides a few convenience methods such as the middleware
method, which may be used to attach middleware to controller actions:
module app.controller.HelloController;
import hunt.framework;
class HelloController : Controller
{
// must be here
mixin MakeController;
@Action
string world()
{
return "Hello world!";
}
}
You can define a route to this controller action like so:
GET /user/world user.world
Now, when a request matches the specified route URI, the world
method on the HelloController
class will be executed. The route parameters will also be passed to the method.
{tip} Controllers are not required to extend a base class. However, you will not have access to convenience features such as the
middleware
,validate
, anddispatch
methods.
Middleware may be assigned to the controller files:
import app.component.middleware.AccountMiddleware;
However, it is more convenient to specify middleware within your controller's constructor. Using the middleware
method from your controller's constructor, you may easily assign middleware to the controller's action.
module app.controller.HelloController;
import hunt.framework;
import app.component.middleware.AccountMiddleware;
class HelloController : Controller
{
// must be here
mixin MakeController;
this() {
addMiddleware(new AccountMiddleware(["user.world"]);
}
@Action
string world()
{
return "Hello world!";
}
}
Controller have two property request and response,look: Request and Response。
- return
string
@Action string showString()
{
return "Hello world.";
}
- return int、float
@Action int showInt()
{
return 2018;
}
- don't return
@Action void showString()
{
// do nothing;
}
- return
bool
@Action bool showBool()
{
return true;
}
- return
Response
@Action Response showResponse()
{
return new Response("Hello world.");
}
- return
JsonResponse
@Action JsonResponse testJson2()
{
JSONValue company;
company["name"] = "Putao";
JsonResponse res = new JsonResponse(company);
return res;
}
- return custom Response object
RedirectResponse
@Action RedirectResponse testRedirect()
{
RedirectResponse r = new RedirectResponse("https://github.com/huntlabs/hunt-framework/");
return r;
}
The hunt-framework resource allocates a typical "crud" route to the controller through a routing file configuration, such as you may wish to create a controller and settings routing that handles HTTP requests "welcome" for print "welcome to hunt-framework" by your application:
- Create a controller
IndexController.d
and create an actiontestWelcome
:
module app.controller.IndexController;
import hunt.framework;
class IndexController : Controller {
mixin MakeController;
@Action
string testWelcome() {
return "welcome to hunt-framework";
}
}
- You may register a resourceful route to the controller:
* /welcome index.testWelcome
More routing settings, please check this.
Controller can verify parameters and forms, please check this.