[关闭]
@fzbing 2015-07-10T12:52:00.000000Z 字数 3591 阅读 1295

路由

学习笔记之Lumen


基本路由

你将在 app/Http/routes.php file 为你的应用定义大多数的路由. 这个文件被 bootstrap/app.php 加载.
Laravel,Lumen路由仅仅支持 URIClosure.

基本GET路由

  1. $app->get('/', function() {
  2. return 'Hello World';
  3. });

其它基本路由

  1. $app->post('foo/bar', function() {
  2. return 'Hello World';
  3. });
  4. $app->patch('foo/bar', function() {
  5. //
  6. });
  7. $app->put('foo/bar', function() {
  8. //
  9. });
  10. $app->delete('foo/bar', function() {
  11. //
  12. });

你将经常性的需要为你的路由 生成URL ,你将用url helper这样做:

  1. $url = url('foo');

指向Controller的路由

如果你对指向Controller的路由感兴趣,请参见文档controllers

路由参数

当然你可以在路由中捕获URI的部分,作为参数.

基本路由参数

  1. $app->get('user/{id}', function($id) {
  2. return 'User '.$id;
  3. });

正则表达式约束

Note: This is the only portion of Lumen that is not directly portable to the full Laravel framework. If you choose to upgrade your Lumen application to Laravel, your regular expression constraints must be moved to a where method call on the route.
唯一一部分与Laravel不一致.

  1. $app->get('user/{name:[A-Za-z]+}', function($name) {
  2. //
  3. });

路由的命名

路由的命名允许你方便的生成 URLs 或者跳转到指定的路由.你可以用 as作为key的一个数组 为一个路由命名.

  1. $app->get('user/profile', ['as' => 'profile', function() {
  2. //
  3. }]);

你也可以为controller actions指定路由名

  1. $app->get('user/profile', [
  2. 'as' => 'profile', 'uses' => 'UserController@showProfile'
  3. ]);

当生成 URL或者跳转,你也可以用路由名了.

  1. $url = route('profile');
  2. $redirect = redirect()->route('profile');

路由分组

有时,你可能想要为 一组路由 应用中间件(middleware).而不是为每一个路由都指定一次middleware.你可以用 route group.

共享属性(Shared attributes)在一个格式化的数组中被指定,作为$app->group()方法的第一个属性

Middleware (中间件)

Middleware is applied to all routes within the group by defining the list of middleware with the middleware parameter on the group attribute array. Middleware will be executed in the order you define this array:

middleware参数在群组属性数组(the group attribute array)中 定义一些middleware,中间件将被应用到所有组(group)的路由. 中间件将按照你定义在数组中的顺序的执行.

  1. $app->group(['middleware' => 'foo|bar'], function($app)
  2. {
  3. $app->get('/', function() {
  4. // Uses Foo & Bar Middleware
  5. });
  6. $app->get('user/profile', function() {
  7. // Uses Foo & Bar Middleware
  8. });
  9. });

命名空间 (Namespaces)

你也许会在群组组属性数组(your group attribute array)中为所有的控制器(controllers)用命名空间参数(the namespace parameter).

  1. $app->group(['namespace' => 'Admin'], function() {
  2. // Controllers Within The "App\Http\Controllers\Admin" Namespace
  3. });

CSRF防护 (CSRF Protection)

Lumen像Laravel一样,很容易的保护你的网站避免 跨网站请求伪造(cross-site request forgeries);跨网站请求伪造是一种攻击.该攻击借由认证过的用户的执行未授权的命令.

Lumen自动在每一个用户的session中生成一个CSRF "token".这个CSRF "token"被用来是验证合法的用户一次实际的请求.

在表单中插入 CSRF token

  1. <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">

当然, 可以用 Blade 模板引擎 (Blade templating engine):

  1. <input type="hidden" name="_token" value="{{ csrf_token() }}">

X-CSRF-TOKEN

除了寻找 CSRF token 作为「POST」参数,中间件也检查 X-XSRF-TOKEN 请求头,比如,你可以把 token 存放在 meta 标签中, 然后使用 jQuery 将它加入到所有的请求头中:

  1. <meta name="csrf-token" content="{{ csrf_token() }}" />
  2. $.ajaxSetup({
  3. headers: {
  4. 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
  5. }
  6. });

现在所有的 AJAX 请求会自动加入 CSRF token:

  1. $.ajax({
  2. url: "/foo/bar",
  3. });

X-XSRF-TOKEN

Laravel 也在 cookie 中存放了名为 XSRF-TOKEN 的 CSRF token。你可以使用这个 cookie 值来设置 X-XSRF-TOKEN 请求头。。一些 Javascript 框架,比如 Angular ,会自动设置这个值。

注意: X-CSRF-TOKENX-XSRF-TOKEN 的不同点在于前者使用的是纯文本而后者是一个加密的值,因为在 Lumen 中 cookies 始终是被加密过的,前提是在 bootstrap/app.php 文件使全局中间件生效。

HTTP请求方法欺骗 (Method Spoofing)

HTML 表单不支持PUTPATCHDELETE请求。所以当定义PUTPATCH 以及DELETE路由并在HTML 表单中调用的时,您将需要在表单中添加隐藏 _method 字段。

发送的_method字段对应的值会被当做HTTP请求方法。举例来说:

  1. <form action="/foo/bar" method="POST">
  2. <input type="hidden" name="_method" value="PUT">
  3. <input type="hidden" name="_token" value="{{ csrf_token() }}">
  4. </form>

抛404错误

有两种方式从路由中手动触发一个404错误.
第一,你可以用the abort helper:

  1. abort(404);

The abort helper 仅仅 抛出一个指定的状态码(the specified status code)的 Symfony\Component\HttpFoundation\Exception\HttpException

第二, 你可以手动抛出一个 Symfony\Component\HttpKernel\Exception\NotFoundHttpException的实例.

更多的处理404异常和应用自定义错误相应(responses)的信息,可以参见错误部分的文档.

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注