Skip to content

Latest commit

 

History

History
192 lines (139 loc) · 4.98 KB

controllers.md

File metadata and controls

192 lines (139 loc) · 4.98 KB

Controllers

Introduction

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.

Basic Controllers

Defining Controllers

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, and dispatch methods.

Controller Middleware

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!";
        }
    }

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. return 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://github.com/huntlabs/hunt-framework/");
        return r;
    }

Resource Controllers

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:

  1. Create a controller IndexController.d and create an action testWelcome:
module app.controller.IndexController;

import hunt.framework;

class IndexController : Controller {

    mixin MakeController;

    @Action 
    string testWelcome() {
        return "welcome to hunt-framework";
    }
}
  1. You may register a resourceful route to the controller:
*     /welcome      index.testWelcome

More routing settings, please check this.

Params Validation and From Validation

Controller can verify parameters and forms, please check this.