可捕获致命错误: 传递给Illuminate\Routing\UrlGenerator::__construct()的第2个参数必须是Illuminate\Http\Request的一个实例,null却被给出。

11

当我尝试运行php artisan (anything)时,我遇到了这个错误:

PHP Catchable fatal error:  Argument 2 passed to
Illuminate\Routing\UrlGenerator::__construct()
must be an instance of Illuminate\Http\Request, null given, called in
/www/laravel5/vendor/laravel/framework/src/Illuminate/Routing/RoutingServiceProvider.php
on line 56 and defined in
/www/laravel5/vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php
on line 81

Catchable fatal error: Argument 2 passed to
Illuminate\Routing\UrlGenerator::__construct()
must be an instance of Illuminate\Http\Request, null given, called in
/www/laravel5/vendor/laravel/framework/src/Illuminate/Routing/RoutingServiceProvider.php
on line 56 and defined in
/www/laravel5/vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php
on line 81

我完全不知道是什么原因导致的,需要帮助。

提前致谢。

4个回答

30

我发现了导致错误的原因。

config/services.php中,我做了这个操作:

'facebook' => [
    'client_id'     => env('FACEBOOK_APP_ID', null),
    'client_secret' => env('FACEBOOK_APP_SECRET', null),
    'redirect'      => url('auth/facebook'),
]

url('auth/facebook') 是导致错误的原因。


谢谢,我也遇到了类似的问题。你是在配置文件中硬编码了完整的URL吗?还是找到了一些传递生成的URL的方法? - Azamat
3
搞定了。使用.env文件来存储应用程序URL,并在服务配置中调用它。 - Azamat

10

正如您已经发现的那样,问题是由于在配置中使用url()引起的。如果使用asset(),也会出现同样的问题。在运行artisan命令时,框架无法确定网站的url,因此会出现错误。

我只是想提出另一种解决方案:

'facebook' => [
  'client_id'     => '***'
  'client_secret' => '***',
  'redirect'      => PHP_SAPI === 'cli' ? false : url('/fb-callback-path'),
]

我不喜欢它,但当你运行命令行脚本时,你很可能永远不会需要你的FB重定向,这样你就不必记住在每个环境中配置重定向。


0
问题是在配置文件中使用了url()函数。 我从config/filesystems.php文件中移除了它,问题解决了! 希望能帮到你!

0

当你在配置文件中使用route()助手时,会出现相同的错误,这就是我的错误。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接