laravel use $app inside routes error Undefined variable: app - BOOKC -->

laravel use $app inside routes error Undefined variable: app

Wednesday, November 6, 2019

laravel 5.2 documentation has used $app->get(..) in routing section. i cant use $app inside routes , it throw below exception:
ErrorException in routes.php line 15: Undefined variable: app


There is no such global variable as $app in Laravel. You are probably referring to Lumen.
Laravel framework at is different from Lumen framework at
Lumen is 'slim down' version of Laravel
If you are trying to register a route in Laravel this will do.
Route::get('/', function () { return 'Hello World'; });
Want to use variable?Try
$router->get('/', function () { return 'Hello World'; });
If you are interested in accessing app, which is not used for routing, try
$app = app();


The documentation you've linked to is for Lumen, not Laravel. Lumen is different than Laravel, and actually uses a completely different router than Laravel, so the routing is setup a little differently.
Routing in Laravel uses the Route facade. For example:
Route::get('foo', function () { return 'Hello World'; });