使用 lumen 文档中的示例代码时未定义的变量 $router

Undefined variable $router when using example code from lumen documentation

我目前正在尝试使用 lumem 框架构建网络 api。我跟随 this tutorial 到达了必须更改路线的部分。唯一的问题是 $router 变量未定义,因此我的网址无法正常工作(错误 404)。

web.php

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It is a breeze. Simply tell Lumen the URIs it should respond to
| and give it the Closure to call when that URI is requested.
|
*/
$router->get('/', function () use ($router) {
    return $router->app->version();
});

$router->group(['prefix' => 'api'], function () use ($router) {
    $router->get('authors',  ['uses' => 'AuthorController@showAllAuthors']);

    $router->get('authors/{id}', ['uses' => 'AuthorController@showOneAuthor']);

    $router->post('authors', ['uses' => 'AuthorController@create']);

    $router->delete('authors/{id}', ['uses' => 'AuthorController@delete']);

    $router->put('authors/{id}', ['uses' => 'AuthorController@update']);
});

我试图用 $app 更改 $router 变量,但这没有任何意义,因为我使用的是 lumen 5.8 并且在 lumen 5.4 中添加了 $router(我认为?)

我是不是做错了什么?谢谢。

只需更改 $router$router->app

阅读更多laravel路由:Docs

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () use () {
    return App::version();
});

Route::group(['prefix' => 'api'], function () use () {
    Route::get('authors',  ['uses' => 'AuthorController@showAllAuthors']);

    Route::get('authors/{id}', ['uses' => 'AuthorController@showOneAuthor']);

    Route::post('authors', ['uses' => 'AuthorController@create']);

    Route::delete('authors/{id}', ['uses' => 'AuthorController@delete']);

    Route::put('authors/{id}', ['uses' => 'AuthorController@update']);
});

所以...

我的问题解决了,我真的不知道如何解决。路由在我没有做任何更改的情况下开始工作,但是我的 IDE (PHPStorm) 没有获取 $router 变量,它是未定义的。

无论如何,谢谢你帮助我。

编辑:我没有使用正确的URL。真丢脸

你应该喜欢这个:

$this->app->router->get('/test', function (){
    return 1;
});