【问题标题】:Problem not recognizing $router variable in lumen无法识别流明中的 $router 变量的问题
【发布时间】:2021-05-07 13:54:47
【问题描述】:

我正在以模块化的方式创建模块。

但是定义新路由有一个问题,就是$router变量在新路由中没有识别出来。

但是如果这样使用,问题就解决了:$this->app->router->get()

但我想使用 $router 变量,例如 web.php 路由默认值。有人可以帮忙吗?

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';
});

【问题讨论】:

    标签: php lumen modular lumenworks


    【解决方案1】:

    您不能在自己的页面中使用 $route 变量。如果你想缩短代码,你可以尝试如下使用。

    $route = $this-app->router;
    

    【讨论】:

    • 是的,你是对的。但是为什么要在 ‍‍web.php‍ ‍‍‍ 文件中使用相同的方法呢?
    猜你喜欢
    • 1970-01-01
    • 2021-05-08
    • 2019-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-06
    • 2018-11-12
    • 1970-01-01
    相关资源
    最近更新 更多