【问题标题】:Laravel: Artisan command error while using request inside config fileLaravel:在配置文件中使用请求时出现 Artisan 命令错误
【发布时间】:2017-11-06 05:29:42
【问题描述】:

我在我的url_prefix 下我安装的包laravel-filemanager 的配置文件中使用了request() 我需要设置目标取决于我的url 所以我使用了request()->segment(2) 一切工作文件除了不能使用工匠命令我收到一个错误显示

[反射异常] 类请求不存在

use Illuminate\Support\Facades\Request 的使用效果不佳

lfm.php

return [
    /*
    |--------------------------------------------------------------------------
    | Routing
    |--------------------------------------------------------------------------
    */

    // Include to pre-defined routes from package or not. Middlewares
    'use_package_routes' => true,

    // Middlewares which should be applied to all package routes.
    // For laravel 5.1 and before, remove 'web' from the array.
    'middlewares' => ['web'],

    // The url to this package. Change it if necessary.
    'url_prefix' => 'laravel-filemanager/'.request()->segment(2),

因此,为了能够使用工匠命令,我正在做的是暂时删除request()->segment(2),并在使用完后将其带回来非常麻烦。有没有办法解决这个问题?

【问题讨论】:

  • 你不能在你的配置文件中使用请求对象——它们是在应用程序启动并且请求对象尚未设置时加载的。
  • @jedrzej.kurylo 谢谢,还有其他方法吗?
  • 在服务提供者上设置前缀
  • @jedrzej.kurylo 关于 add first 有两个问题:配置/会话是否在每个请求上运行?并设置这个 'secure' => env('SESSION_SECURE_COOKIE', true), second:这里有没有办法根据 URL 将它设置为 true 或 false

标签: php laravel laravel-5.5


【解决方案1】:

由于 Request 类仅适用于 Http 请求,因此您不应该在配置文件等中使用它。 让我们在 AppServicePrivider.php 启动方法中启动 url_prefix:

public function boot()
{
    config(['app.url_prefix' => 'laravel-filemanager/'.request()->segment(2)]);
}

【讨论】:

    猜你喜欢
    • 2012-06-16
    • 2017-01-18
    • 2022-01-05
    • 2017-03-26
    • 2015-12-24
    • 2015-10-09
    • 1970-01-01
    • 2016-12-08
    • 2017-01-04
    相关资源
    最近更新 更多