简体   繁体   English

Laravel view.php在base_path中使用config :: get变量

[英]Laravel view.php use config::get variable in base_path

I am trying to do this in laravel 5.2 view.php (edit base_path to use a config variable in the string): 我试图在laravel 5.2 view.php中执行此操作(编辑base_path以在字符串中使用配置变量):

<?php

use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Request;

return [

/*
|--------------------------------------------------------------------------
| View Storage Paths
|--------------------------------------------------------------------------
|
| Most templating systems load templates from disk. Here you may specify
| an array of paths that should be checked for your views. Of course
| the usual Laravel view path has already been registered for you.
|
*/

'paths' => [
    realpath(base_path('resources/views/layouts/' . Config::get('api.' . Request::get('domain') . '.layout'))),
],

But now I receive this error: 但是现在我收到此错误:

Fatal error: Uncaught exception 'ReflectionException' with message 'Class log does not exist' in /Applications/AMPPS/www/loan/vendor/laravel/framework/src/Illuminate/Container/Container.php:734 Stack trace: #0 /Applications/AMPPS/www/loan/vendor/laravel/framework/src/Illuminate/Container/Container.php(734): ReflectionClass->__construct('log') #1 /Applications/AMPPS/www/loan/vendor/laravel/framework/src/Illuminate/Container/Container.php(629): Illuminate\\Container\\Container->build('log', Array) #2 /Applications/AMPPS/www/loan/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(697): Illuminate\\Container\\Container->make('log', Array) #3 /Applications/AMPPS/www/loan/vendor/laravel/framework/src/Illuminate/Container/Container.php(849): Illuminate\\Foundation\\Application->make('Psr\\Log\\LoggerI...') #4 /Applications/AMPPS/www/loan/vendor/laravel/framework/src/Illuminate/Container/Container.php(804): Illuminate\\Container\\Container->resolveClass(Object(ReflectionParameter)) #5 /A 致命错误:/Applications/AMPPS/www/loan/vendor/laravel/framework/src/Illuminate/Container/Container.php:734中未捕获的异常“ Re​​flectionException”,消息为“类日志不存在”:堆栈堆栈跟踪:#0 /应用程序/AMPPS/www/loan/vendor/laravel/framework/src/Illuminate/Container/Container.php(734):ReflectionClass->__construct('log')#1 / Applications / AMPPS / www / loan / vendor / laravel /framework/src/Illuminate/Container/Container.php(629):Illuminate\\Container\\Container->build('log',Array)#2 / Applications / AMPPS / www / loan / vendor / laravel / framework / src / Illuminate / Foundation / Application.php(697):Illuminate \\ Container \\ Container-> make('log',Array)#3 / Applications / AMPPS / www / loan / vendor / laravel / framework / src / Illuminate / Container / Container .php(849):Illuminate \\ Foundation \\ Application-> make('Psr \\ Log \\ LoggerI ...')#4 / Applications / AMPPS / www / loan / vendor / laravel / framework / src / Illuminate / Container / Container .php(804):照明\\容器\\容器-> resolveClass(Object(ReflectionParameter))#5 / A pplications/AMPPS/www/loan/vendor/l in /Applications/AMPPS/www/loan/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 734 /Applications/AMPPS/www/loan/vendor/laravel/framework/src/Illuminate/Container/Container.php上的pplications / AMPPS / www / loan / vendor / l

How do I fix this? 我该如何解决? Because everything I try doesn't work. 因为我尝试的所有方法都不起作用。 Thanks ahead of time! 提前谢谢!

Short answer: yes. 简短的回答:是的。 Add this to the top of the file: 将此添加到文件顶部:

use Illuminate\Support\Facades\Config;

You need to move this logic out to your ViewServiceProvider instead of trying to do this directly in the config file, that's a big no no. 您需要将此逻辑移到ViewServiceProvider而不是尝试直接在配置文件中执行此操作,这是很大的不。

So what we're going to do is 所以我们要做的是

php artisan make:provider MyViewServiceProvider

Which is going to result in a file existing at: 这将导致文件存在于:

App\Providers\MyViewServiceProvider

Now we're going to open config/app.php . 现在,我们将打开config/app.php Find the existing ViewServiceProvider::class in this file and replace it with the namespaced path above. 在此文件中找到现有的ViewServiceProvider::class并将其替换为上面的命名空间。 It should look something like this: 它看起来应该像这样:

//the old Illuminate\View\ViewServiceProvider::class
App\Providers\MyViewServiceProvider::class,

Now inside of the registerViewFinder() function, we can overload our view paths. 现在,在registerViewFinder()函数内部,我们可以重载视图路径。

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Config;

public function registerViewFinder()
{
    $this->app->bind('view.finder', function ($app) {
        $custom_path = base_path('resources/views/layouts/' . Config::get('api.' . $this->app->request()->get('domain') . '.layout')
        $paths = array_merge(
            [$custom_path],
            $app['config']['view.paths']
        );

        return new FileViewFinder($app['files'], $paths);
    });
}

Going this route will ensure that your path is observed first. 走这条路线将确保首先观察您的路径。 If the view is not found in that path, then you can fallback to Laravel 's default view path. 如果在该路径中找不到该视图,则可以回Laravel的默认视图路径。

Edit 编辑

It's important to note that your class needs to extend the default ViewServiceProvider, and that there are 2 other functions you must declare, the whole file should look like below: 重要的是要注意,您的类需要扩展默认的ViewServiceProvider,并且还必须声明两个其他函数,整个文件应如下所示:

<?php

namespace App\Providers;

use Illuminate\View\ViewServiceProvider;
use Illuminate\Support\Facades\Config;

class MyViewServiceProvider extends ViewServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        parent::boot();
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        parent::register();
    }

    /**
     * Register the view finder implementation.
     *
     * @return void
     */
    public function registerViewFinder()
    {
        $this->app->bind('view.finder', function ($app) {
            $custom_path = base_path('resources/views/layouts/' . Config::get('api.' . $this->app->request->get('domain') . '.layout')
            $paths = array_merge(
                [$custom_path],
                $app['config']['view.paths']
            );

            return new FileViewFinder($app['files'], $paths);
        });
    }
}

You can use the config - and request -helpers in your app's config files. 您可以使用配置 -并要求 -helpers在应用程序的配置文件。

'paths' => [
    realpath(base_path(
        'resources/views/layouts/' . config('api.' . request('domain') . '.layout')
    )),
],

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

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