-1

I have a Laravel project, this code from routes/web.php

$router->get('/', function () use ($router) {
    return $router->app->version();
});

When I try to run 'php artisan serve' it gives Undefined property: Illuminate\Routing\Router::$app error,

I tried adding container instead of app

$router->get('/', function () use ($router) {
    return $router->container->version();
});

then it gives 'Error: cannot access protected property' Please let me know if anyone knows how to clear this issue and run the project

ErrorException: Undefined property: Illuminate\Routing\Router::$app

at /Applications/XAMPP/xamppfiles/htdocs/em2/routes/web.php:18 at Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(8, 'Undefined property: Illuminate\Routing\Router::$app', '/Applications/XAMPP/xamppfiles/htdocs/em2/routes/web.php', 18, array('router' => object(Router))) (/Applications/XAMPP/xamppfiles/htdocs/em2/routes/web.php:18) at Illuminate\Routing\RouteFileRegistrar->{closure}()
Sujeevan
  • 5
  • 4

3 Answers3

1

use app() helper method to access app instance from anywhere. try this

$router->get('/', function () {
    return app()->version();
});
aimme
  • 6,385
  • 7
  • 48
  • 65
1

Perhaps you looking for app Version. then use config('app.version'); resp. app()->version(). What @aimme has already said

$router->get('/', function () {
    return config('app.version');
});

OR

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

Both will returned the app version. The App Version you define in the config/app.php.

'version' => env('APP_VERSION', '1.1.1'),

Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79
  • Both methods cleared the issue but not loding the project sorry I am new to this now when I run php artisan serve and that url only gives app version number. Do I missing something? @MaikLowrey – Sujeevan Nov 11 '21 at 10:59
  • @Sujeevan if I understood your comment correctly. As soon as you call your project: `http:localhost:8000`. You will get the app version number. Right :-) – Maik Lowrey Nov 11 '21 at 11:06
  • Yah I got that but I just wanted to run my index.php – Sujeevan Nov 11 '21 at 11:08
0

In routes/web.php use Route Facade for routing.

use Illuminate\Support\Facades\Route;

Route::get('/', function (){
    app()->version();
});