简体   繁体   English

无法识别流明中的 $router 变量的问题

[英]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.但是在定义新路由的时候有一个问题,那就是$router变量在新路由中没有识别出来。

But if used this way, the problem will be solved: $this->app->router->get()但是如果这样使用,问题就解决了: $this->app->router->get()

But I want to use the $router variable like the web.php route default.但我想使用$router变量,如web.php路由默认值。 Can anyone help?任何人都可以帮忙吗?

UserServiceProvider.php UserServiceProvider.php

use Illuminate\Support\ServiceProvider;

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

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.您不能在自己的页面中使用$route变量。 If you are trying to shorten the code, you can try a use as follows.如果你想缩短代码,你可以尝试如下使用。

$route = $this-app->router;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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