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

时间:2019-07-25 14:58:01

标签: php laravel api routes lumen

我目前正在尝试使用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']);
});

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

我做错了吗?谢谢。

3 个答案:

答案 0 :(得分:0)

只需更改$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']);
});

答案 1 :(得分:0)

所以...

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

无论如何,谢谢您的帮助。

答案 2 :(得分:0)

你应该喜欢这个:

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