简体   繁体   English

流明路径组错误:“未定义变量:应用程序”

[英]Lumen route groups error: “Undefined variable: app”

I just created a new lumen application with this very simple routes file: 我刚刚用这个非常简单的路由文件创建了一个新的流明应用程序:

<?php

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


$app->group(['prefix' => '/admin'], function () {

  $app->get('/user', function () {
    return 'Admin user';
  });

});

And I get this error: 我得到这个错误:

lumen.ERROR: exception 'ErrorException' with message 'Undefined variable: app' in /path/to/my/lumen/project/app/Http/routes.php:10

What's wrong? 怎么了?

Note that if I remove the route group everything works great. 请注意,如果删除路由组,一切都会很好。

The problem is the closure you're using in the group call: 问题是您在group调用中使用的闭包:

$app->group(['prefix' => '/admin'], function () {

        $app->get('/user', function () {
        return 'Admin - user';
    });

});

You'll have to pass it a reference to $app : 您必须将对$app的引用传递给它:

$app->group(['prefix' => '/admin'], function () use ($app) {

        $app->get('/user', function () {
        return 'Admin - user';
    });

});

The lumen docs on the laravel website contained an error, but the docs on github have been fixed. laravel网站上的lumen文档包含错误,但github上的文档已修复。 As it turns out, the application instance is passed as an argument to the callback, so you can do away with that use ($app) bit, and instead write this: 事实证明,应用程序实例作为参数传递给回调,因此您可以取消use ($app)位,而可以这样编写:

$app->group(['prefix' => '/admin'], function ($app) {

        $app->get('/user', function () {
        return 'Admin - user';
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM