【问题标题】:Call to undefined method Closure::group() in Lumen在 Lumen 中调用未定义的方法 Closure::group()
【发布时间】:2017-07-12 18:52:34
【问题描述】:

这里是 Lumen Vuejs 的新手。

我对 2 个框架之间的连接有疑问,尤其是在生成令牌方面。

这是我的 App.php

require_once __DIR__.'/../vendor/autoload.php';

try {
    (new Dotenv\Dotenv(__DIR__.'/../'))->load();
} catch (Dotenv\Exception\InvalidPathException $e) {
    //
}

/*
|--------------------------------------------------------------------------
| Create The Application
|--------------------------------------------------------------------------
|
| Here we will load the environment and create the application instance
| that serves as the central piece of this framework. We'll use this
| application as an "IoC" container and router for this framework.
|
*/

$app = new Laravel\Lumen\Application(
    realpath(__DIR__.'/../')
);

$app->withFacades();

$app->withEloquent();

/*
|--------------------------------------------------------------------------
| Register Container Bindings
|--------------------------------------------------------------------------
|
| Now we will register a few bindings in the service container. We will
| register the exception handler and the console kernel. You may add
| your own bindings here if you like or you can make another file.
|
*/

$app->singleton(
    Illuminate\Contracts\Debug\ExceptionHandler::class,
    App\Exceptions\Handler::class
);

$app->singleton(
    Illuminate\Contracts\Console\Kernel::class,
    App\Console\Kernel::class
);

/*
|--------------------------------------------------------------------------
| Register Middleware
|--------------------------------------------------------------------------
|
| Next, we will register the middleware with the application. These can
| be global middleware that run before and after each request into a
| route or middleware that'll be assigned to some specific routes.
|
*/

$app->middleware([
    App\Http\Middleware\ExampleMiddleware::class,
    palanik\lumen\Middleware\LumenCors::class
]);

$app->routeMiddleware([
   'auth' => App\Http\Middleware\Authenticate::class,
   'cors' => palanik\lumen\Middleware\LumenCors::class,
]);

/*
|--------------------------------------------------------------------------
| Register Service Providers
|--------------------------------------------------------------------------
|
| Here we will register all of the application's service providers which
| are used to bind services into the container. Service providers are
| totally optional, so you are not required to uncomment this line.
|
*/

// $app->register(App\Providers\AppServiceProvider::class);
// $app->register(App\Providers\EventServiceProvider::class);
$app->register(App\Providers\AuthServiceProvider::class);
$app->register(Laravel\Passport\PassportServiceProvider::class);
$app->register(Dusterio\LumenPassport\PassportServiceProvider::class);

/*
|--------------------------------------------------------------------------
| Load The Application Routes
|--------------------------------------------------------------------------
|
| Next we will include the routes file so that they can all be added to
| the application. This will provide all of the URLs the application
| can respond to, as well as the controllers that may handle them.
|
*/

$app->group(['namespace' => 'App\Http\Controllers'], function ($app) {
    require __DIR__.'/../routes/web.php';
});

return $app;

这是我的 AuthServiceProvider.php

<?php

namespace App\Providers;

use App\User;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;
use Dusterio\LumenPassport\LumenPassport;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Boot the authentication services for the application.
     *
     * @return void
     */
    public function boot()
    {
        // Here you may define how you wish users to be authenticated for your Lumen
        // application. The callback which receives the incoming request instance
        // should return either a User instance or null. You're free to obtain
        // the User instance via an API token or any other method necessary.

        // Dusterio\LumenPassport\LumenPassport::routes($app);
        LumenPassport::routes();

        $this->app['auth']->viaRequest('api', function ($request) {
            if ($request->input('api_token')) {
                return User::where('api_token', $request->input('api_token'))->first();
            }
        });
    }
}

这里的问题是在 AuthServiceProvider.php 文件中发现的。我已经将这个包https://github.com/dusterio/lumen-passport 用于护照,并将https://github.com/palanik/lumen-cors 用于HTTP 请求的交叉来源,即使我正确地遵循了2 个包的文档,我也不知道错误到底在哪里发生。

非常感谢您的回复!

【问题讨论】:

    标签: php vue.js lumen


    【解决方案1】:

    您可能使用 Lumen 5.5。

    dusterio\lumen-passport 目前与 Lumen 5.5 不兼容(截至 2017 年 10 月 5 日)(请参阅https://github.com/dusterio/lumen-passport/pull/53)

    要么降级到 Lumen 5.4,要么等待这个包更新。

    【讨论】:

    • 知道如何将您的更改实施到我的项目中吗?最好通过作曲家
    【解决方案2】:

    试试这个:

    1. 在你的 app/Providers 文件夹中打开 AuthServiceProvider.php,添加

      use Dusterio\LumenPassport\LumenPassport;
      
    2. 在 boot() 函数中,添加:

      LumenPassport::routes($this->app->router, ['prefix' => 'v1/oauth']);
      

    如果你不想要前缀,那么你可以删除它。

    【讨论】:

      【解决方案3】:

      刚刚找到解决办法。

      打电话-

         Dusterio\LumenPassport\LumenPassport::routes($app);
      

      在 bootstrap/app.php 中注册 Passport 服务提供者后,而不是在文档中定义的 AppServiceProvider 中调用 LumenPassport::routes();。

      然后,转到-

         <app_source>/vendor/dusterio/lumen-passport/src/LumenPassport.php
      

      搜索路线功能

         public static function routes($callback = null, array $options = [])
         {
            ...
         }
      

      寻找——

         $callback->group($options, function ($router) use ($callback) {
              ...
          });
      

      改变 -

          $callback->group(...) to $callback->route->group(...)
      

      这完全解决了问题。

      【讨论】:

      • 这不是正确的解决方案。您不应修改任何供应商文件。 vendor/dusterio/lumen-passport 中存在问题,您应该向软件包维护者提交错误,或者问题出在您的项目中。
      • 当然。我会去做的。例如,我们可以继续解决问题。
      • 你好 Vaishnav。在线程中查看我的答案。 lumen-passport 中已经报告了一个错误。截至 2017 年 10 月 23 日,它尚未修复,但他们正在努力解决。
      • 是的。刚才看到的。我纠正了错误,以便拥有旧仓库的开发人员无需升级即可解决它。
      【解决方案4】:

      我认为是$callback-&gt;router-&gt;group

      【讨论】:

      • 能否请您用相关代码给出更多解释?
      【解决方案5】:

      看新鲜流明项目bootstrap/app.php

      $app->router->group([
          'namespace' => 'App\Http\Controllers',
      ], function ($router) {
          require __DIR__.'/../routes/web.php';
      });
      
      return $app;
      

      Lumen 改变了定义路由的方式。所以,使用新方法。

      • 老:$app-&gt;group
      • 新:$app-&gt;route-&gt;group

      参考Lumen 5.5 - Route Group

      【讨论】:

        猜你喜欢
        • 2018-03-03
        • 2016-05-29
        • 1970-01-01
        • 2019-07-13
        • 1970-01-01
        • 1970-01-01
        • 2020-01-25
        • 2020-09-29
        • 1970-01-01
        相关资源
        最近更新 更多