I'm currently trying to build a web api using the lumem framework. I followed this tutorial and got to the part where I have to change the routes. The only problem is that the $router
variable is undefined and therefore my urls are not working (Error 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']);
});
I tried to change the $router
variable with $app
but this makes no sense, because I am using lumen 5.8 and $router was added in lumen 5.4 (I think ?)
Did I do something wrong? Thanks.
Just change $router
and $router->app
Read more laravel routing here: 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']);
});
So...
My problem is solved, I don't really know how. The routes started working without me changing anything, but my IDE (PHPStorm) doesn't pick up the $router variable, it's undefined.
In any case, thank you for helping me.
Edit: I wasn't using the right URL. Shame on me
You should like this:
$this->app->router->get('/test', function (){
return 1;
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.