简体   繁体   中英

Problem not recognizing $router variable in lumen

I am creating modules in a modular way.

But there is a problem in defining the new route, and that is that the $router variable does not recognize in the new route.

But if used this way, the problem will be solved: $this->app->router->get()

But I want to use the $router variable like the web.php route default. Can anyone help?

UserServiceProvider.php

use Illuminate\Support\ServiceProvider;

class UserServiceProvider extends ServiceProvider
{
    public function boot()
    {
        $this->loadRoutesFrom(__DIR__ . '/../Routes/user_route.php');
    }
}

user_route.php

/** @var Laravel\Lumen\Routing\Router $router */

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

// it works
$this->app->router->get('module/test', function () {
  return 'yohooooo';
});

You cannot use the $route variable in your own page. If you are trying to shorten the code, you can try a use as follows.

$route = $this-app->router;

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM