Skip to content

Controller

Brian Zou edited this page Nov 17, 2018 · 8 revisions

Controller

Files

Add controllers files to this path:

source/app/controller/

Sample class

module app.controller.UserController;

/*
 import hunt framework package
*/
import hunt.framework;

class HelloController : Controller
{
   // must be here
   mixin MakeController;

   @Action
   string world()
   {
      return "Hello world!";
   }
}

Request and Response

Controller have two property request and response,look: Request and Response

Action return types

  1. return string
@Action string showString()
{
    return "Hello world.";
}
  1. return int、float
@Action int showInt()
{
    return 2018;
}
  1. don't return
@Action void showString()
{
    // do nothing;
}
  1. return bool
@Action bool showBool()
{
    return true;
}
  1. returnResponse
@Action Response showResponse()
{
    return new Response("Hello world.");
}
  1. rerturn JsonResponse
@Action JsonResponse testJson2()
{
    JSONValue company;
    company["name"] = "Putao";

    JsonResponse res = new JsonResponse(company);
    return res;
}
  1. return custom Response object RedirectResponse
@Action RedirectResponse testRedirect()
{
    RedirectResponse r = new RedirectResponse("https://www.putao.com/");
    return r;
}
Clone this wiki locally