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



我目前正在尝试使用lumem框架构建一个Web api。我按照本教程进行操作,并进入了必须更改路线的部分。唯一的问题是$router变量未定义,因此我的 url 不起作用(错误 404(。

网络.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 变量,但这毫无意义,因为我使用的是流明 5.8 并且$router是在流明 5.4 中添加的(我认为?

我做错了什么吗?谢谢。

只需更改$router$router->app

在此处阅读更多 laravel 路由: 文档

<?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;
});

最新更新