Skip to content

Commit

Permalink
docs: app.controller.foo instead of 'foo' (#942)
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse authored and popomore committed May 20, 2017
1 parent cfc76ec commit 713e033
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 57 deletions.
44 changes: 23 additions & 21 deletions docs/source/en/basics/router.md
Expand Up @@ -12,7 +12,7 @@ By unifying routing rules, routing logics are free from scatter that may cause u
```js
// app/router.js
module.exports = app => {
app.get('/user/:id', 'user.info');
app.get('/user/:id', app.controller.user.info);
};
```

Expand Down Expand Up @@ -53,7 +53,9 @@ The complete definition of router includes 5 major parts:
- router-name defines a alias for the route, and URL can be generated by helper method `pathFor` and `urlFor` provided by Helper. (Optional)
- path-match - URL path of the route.
- middleware1 - multiple Middleware can be configured in Router. (Optional)
- controller.action - it's a string, take care. The framework will find the Controller sharing the name under `app/controller`, then assign the process to the configured action method. The action can be omitted if the Controller exports a method directly.
- controller - specific the controller of this router, this param can be tow types:
* `app.controller.user.fetch` - directly point to a controller
* `'user.fetch'` - simplified as a string,

### Notices

Expand All @@ -65,11 +67,11 @@ The complete definition of router includes 5 major parts:
Here are some examples of writing routing rules:

```js
app.get('/home', 'home');
app.get('/user/:id', 'user.page');
app.post('/admin', isAdmin, 'admin');
app.post('user', '/user', isLoginUser, hasAdminPermission, 'user.create');
app.post('/api/v1/comments', 'v1.comments.create'); // app/controller/v1/comments.js
app.get('/home', app.controller.home);
app.get('/user/:id', app.controller.user.page);
app.post('/admin', isAdmin, app.controller.admin);
app.post('user', '/user', isLoginUser, hasAdminPermission, app.controller.user.create);
app.post('/api/v1/comments', app.controller.v1.comments.create); // app/controller/v1/comments.js
```

### RESTful style URL definition
Expand All @@ -79,8 +81,8 @@ We provide `app.resources('router-name', 'path-match', 'controller-name')` to ge
```js
// app/router.js
module.exports = app => {
app.resources('posts', '/posts', 'posts');
app.resources('users', '/api/v1/users', 'v1.users'); // app/controller/v1/users.js
app.resources('posts', '/posts', app.controller.posts);
app.resources('users', '/api/v1/users', app.controller.v1.users); // app/controller/v1/users.js
};
```

Expand Down Expand Up @@ -126,7 +128,7 @@ More practical examples will be shown below to demonstrate how to use the router
```js
// app/router.js
module.exports = app => {
app.get('/search', 'search');
app.get('/search', app.controller.search);
};

// app/controller/search.js
Expand All @@ -142,7 +144,7 @@ module.exports = function* (ctx) {
```js
// app/router.js
module.exports = app => {
app.get('/user/:id/:name', 'user.info');
app.get('/user/:id/:name', app.controller.user.info);
};

// app/controller/user.js
Expand All @@ -160,7 +162,7 @@ Regular expressions, as well, can be used in routing rules to aquire parameters
```js
// app/router.js
module.exports = app => {
app.get(/^\/package\/([\w-.]+\/[\w-.]+)$/, 'package.detail');
app.get(/^\/package\/([\w-.]+\/[\w-.]+)$/, app.controller.package.detail);
};

// app/controller/package.js
Expand All @@ -178,7 +180,7 @@ exports.detail = function* (ctx) {
```js
// app/router.js
module.exports = app => {
app.post('/form', 'form');
app.post('/form', app.controller.form);
};

// app/controller/form.js
Expand Down Expand Up @@ -212,7 +214,7 @@ exports.security = {
```js
// app/router.js
module.exports = app => {
app.post('/user', 'user');
app.post('/user', app.controller.user);
};

// app/controller/user.js
Expand Down Expand Up @@ -242,7 +244,7 @@ exports.create = function* (ctx) {
```js
// app/router.js
module.exports = app => {
app.get('index', '/home/index', 'home.index');
app.get('index', '/home/index', app.controller.home.index);
app.redirect('/', '/home/index', 302);
};

Expand All @@ -259,7 +261,7 @@ exports.index = function* (ctx) {
```js
// app/router.js
module.exports = app => {
app.get('/search', 'search');
app.get('/search', app.controller.search);
};

// app/controller/search.js
Expand Down Expand Up @@ -299,7 +301,7 @@ module.exports = () => {

// app/router.js
module.exports = app => {
app.get('s', '/search', app.middlewares.uppercase(), 'search')
app.get('s', '/search', app.middlewares.uppercase(), app.controller.search)
};

// curl http://localhost:7001/search2?name=egg
Expand All @@ -320,13 +322,13 @@ module.exports = app => {

// app/router/news.js
module.exports = app => {
app.get('/news/list', 'news.list');
app.get('/news/detail', 'news.detail');
app.get('/news/list', app.controller.news.list);
app.get('/news/detail', app.controller.news.detail);
};

// app/router/admin.js
module.exports = app => {
app.get('/admin/user', 'admin.user');
app.get('/admin/log', 'admin.log');
app.get('/admin/user', app.controller.admin.user);
app.get('/admin/log', app.controller.admin.log);
};
```
6 changes: 3 additions & 3 deletions docs/source/en/intro/quickstart.md
Expand Up @@ -86,7 +86,7 @@ Then edit the router file and add a mapping.
```js
// app/router.js
module.exports = app => {
app.get('/', 'home.index');
app.get('/', app.controller.home.index);
};
```

Expand Down Expand Up @@ -228,8 +228,8 @@ module.exports = app => {

// app/router.js
module.exports = app => {
app.get('/', 'home.index');
app.get('/news', 'news.list');
app.get('/', app.controller.home.index);
app.get('/news', app.controller.news.list);
};
```

Expand Down
6 changes: 3 additions & 3 deletions docs/source/zh-cn/basics/controller.md
Expand Up @@ -53,12 +53,12 @@ module.exports = app => {
}
```

我们通过上面的代码定义了一个 `PostController` 的类,类里面的每一个方法都可以作为一个 Controller 在 Router 中引用到。
我们通过上面的代码定义了一个 `PostController` 的类,类里面的每一个方法都可以作为一个 Controller 在 Router 中引用到,我们可以从 `app.controller` 根据文件名和方法名定位到它

```js
// app/router.js
module.exports = app => {
app.post('createPost', '/api/posts', 'post.create');
app.post('createPost', '/api/posts', app.controller.post.create);
}
```

Expand All @@ -67,7 +67,7 @@ Controller 支持多级目录,例如如果我们将上面的 Controller 代码
```js
// app/router.js
module.exports = app => {
app.post('createPost', '/api/posts', 'sub.post.create');
app.post('createPost', '/api/posts', app.controller.sub.post.create);
}
```

Expand Down
55 changes: 28 additions & 27 deletions docs/source/zh-cn/basics/router.md
Expand Up @@ -13,7 +13,7 @@ Router 主要用来描述请求 URL 和具体承担执行动作的 Controller
```js
// app/router.js
module.exports = app => {
app.get('/user/:id', 'user.info');
app.get('/user/:id', app.controller.user.info);
};
```

Expand All @@ -35,10 +35,10 @@ exports.info = function* (ctx) {
下面是路由的完整定义,参数可以根据场景的不同,自由选择:

```js
app.verb('path-match', 'controller.action');
app.verb('router-name', 'path-match', 'controller.action');
app.verb('path-match', middleware1, ..., middlewareN, 'controller.action');
app.verb('router-name', 'path-match', middleware1, ..., middlewareN, 'controller.action');
app.verb('path-match', app.controller.controller.action);
app.verb('router-name', 'path-match', app.controller.controller.action);
app.verb('path-match', middleware1, ..., middlewareN, app.controller.controller.action);
app.verb('router-name', 'path-match', middleware1, ..., middlewareN, app.controller.controller.action);
```

路由完整定义主要包括5个主要部分:
Expand All @@ -56,8 +56,9 @@ app.verb('router-name', 'path-match', middleware1, ..., middlewareN, 'controller
- router-name 给路由设定一个别名,可以通过 Helper 提供的辅助函数 `pathFor``urlFor` 来生成 URL。(可选)
- path-match - 路由 URL 路径。
- middleware1 - 在 Router 里面可以配置多个 Middleware。(可选)
- controller.action - 注意是字符串,框架会自动从 `app/controller` 目录中去查找同名 Controller,
并且把处理指定到配置的 action 方法。如果 Controller 文件直接 export 一个方法,可以省略 action。
- controller - 指定路由映射到的具体的 controller 上,controller 可以有两种写法:
* `app.controller.user.fetch` - 直接指定一个具体的 controller
* `'user.fetch'` - 可以简写为字符串形式

### 注意事项

Expand All @@ -69,23 +70,23 @@ app.verb('router-name', 'path-match', middleware1, ..., middlewareN, 'controller
下面是一些路由定义的方式:

```js
app.get('/home', 'home');
app.get('/user/:id', 'user.page');
app.post('/admin', isAdmin, 'admin');
app.post('/user', isLoginUser, hasAdminPermission, 'user.create');
app.post('/api/v1/comments', 'v1.comments.create'); // app/controller/v1/comments.js
app.get('/home', app.controller.home);
app.get('/user/:id', app.controller.user.page);
app.post('/admin', isAdmin, app.controller.admin);
app.post('/user', isLoginUser, hasAdminPermission, app.controller.user.create);
app.post('/api/v1/comments', app.controller.v1.comments.create); // app/controller/v1/comments.js
```

### RESTful 风格的 URL 定义

如果想通过 RESTful 的方式来定义路由,
我们提供了 `app.resources('routerName', 'pathMatch', 'controllerName')` 快速在一个路径上生成 [CRUD](https://en.wikipedia.org/wiki/Create,_read,_update_and_delete) 路由结构。
我们提供了 `app.resources('routerName', 'pathMatch', controller)` 快速在一个路径上生成 [CRUD](https://en.wikipedia.org/wiki/Create,_read,_update_and_delete) 路由结构。

```js
// app/router.js
module.exports = app => {
app.resources('posts', '/api/posts', 'posts');
app.resources('users', '/api/v1/users', 'v1.users'); // app/controller/v1/users.js
app.resources('posts', '/api/posts', app.controller.posts);
app.resources('users', '/api/v1/users', app.controller.v1.users); // app/controller/v1/users.js
};
```

Expand Down Expand Up @@ -132,7 +133,7 @@ exports.destroy = function* () {};
```js
// app/router.js
module.exports = app => {
app.get('/search', 'search');
app.get('/search', app.controller.search);
};

// app/controller/search.js
Expand All @@ -148,7 +149,7 @@ module.exports = function* (ctx) {
```js
// app/router.js
module.exports = app => {
app.get('/user/:id/:name', 'user.info');
app.get('/user/:id/:name', app.controller.user.info);
};

// app/controller/user.js
Expand All @@ -166,7 +167,7 @@ exports.info = function* (ctx) {
```js
// app/router.js
module.exports = app => {
app.get(/^\/package\/([\w-.]+\/[\w-.]+)$/, 'package.detail');
app.get(/^\/package\/([\w-.]+\/[\w-.]+)$/, app.controller.package.detail);
};

// app/controller/package.js
Expand All @@ -184,7 +185,7 @@ exports.detail = function* (ctx) {
```js
// app/router.js
module.exports = app => {
app.post('/form', 'form');
app.post('/form', app.controller.form);
};

// app/controller/form.js
Expand Down Expand Up @@ -218,7 +219,7 @@ exports.security = {
```js
// app/router.js
module.exports = app => {
app.post('/user', 'user');
app.post('/user', app.controller.user);
};

// app/controller/user.js
Expand Down Expand Up @@ -248,7 +249,7 @@ exports.create = function* (ctx) {
```js
// app/router.js
module.exports = app => {
app.get('index', '/home/index', 'home.index');
app.get('index', '/home/index', app.controller.home.index);
app.redirect('/', '/home/index', 302);
};

Expand All @@ -265,7 +266,7 @@ exports.index = function* (ctx) {
```js
// app/router.js
module.exports = app => {
app.get('/search', 'search');
app.get('/search', app.controller.search);
};

// app/controller/search.js
Expand Down Expand Up @@ -305,7 +306,7 @@ module.exports = () => {

// app/router.js
module.exports = app => {
app.get('s', '/search', app.middlewares.uppercase(), 'search')
app.get('s', '/search', app.middlewares.uppercase(), app.controller.search)
};

// curl http://localhost:7001/search2?name=egg
Expand All @@ -326,13 +327,13 @@ module.exports = app => {

// app/router/news.js
module.exports = app => {
app.get('/news/list', 'news.list');
app.get('/news/detail', 'news.detail');
app.get('/news/list', app.controller.news.list);
app.get('/news/detail', app.controller.news.detail);
};

// app/router/admin.js
module.exports = app => {
app.get('/admin/user', 'admin.user');
app.get('/admin/log', 'admin.log');
app.get('/admin/user', app.controller.admin.user);
app.get('/admin/log', app.controller.admin.log);
};
```
6 changes: 3 additions & 3 deletions docs/source/zh-cn/intro/quickstart.md
Expand Up @@ -78,7 +78,7 @@ module.exports = app => {
```js
// app/router.js
module.exports = app => {
app.get('/', 'home.index');
app.get('/', app.controller.home.index);
};
```

Expand Down Expand Up @@ -212,8 +212,8 @@ module.exports = app => {

// app/router.js
module.exports = app => {
app.get('/', 'home.index');
app.get('/news', 'news.list');
app.get('/', app.controller.home.index);
app.get('/news', app.controller.news.list);
};
```

Expand Down

0 comments on commit 713e033

Please sign in to comment.