Laravel view.php在base_path

时间:2016-08-03 16:54:13

标签: php laravel request config base-path

我试图在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'))),
],

但现在我收到了这个错误:

  

致命错误:未捕获的异常&#39; ReflectionException&#39;消息&#39;班级日志不存在&#39;在/Applications/AMPPS/www/loan/vendor/laravel/framework/src/Illuminate/Container/Container.php:734堆栈跟踪:#0 / Applications / AMPPS / www / loan / vendor / laravel / framework / src / Illuminate /Container/Container.php(734):ReflectionClass-&gt; __ construct(&#39; log&#39;)#1 / Applications / AMPPS / www / loan / vendor / laravel / framework / src / Illuminate / Container / Container。 php(629):Illuminate \ Container \ Container-&gt; build(&#39; log&#39;,Array)#2 / Applications / AMPPS / www / loan / vendor / laravel / framework / src / Illuminate / Foundation / Application .php(697):Illuminate \ Container \ Container-&gt; make(&#39; log&#39;,Array)#3 / Applications / AMPPS / www / loan / vendor / laravel / framework / src / Illuminate / Container / Container.php(849):Illuminate \ Foundation \ Application-&gt; make(&#39; Psr \ Log \ LoggerI ...&#39;)#4 / Applications / AMPPS / www / loan / vendor / laravel / framework /src/Illuminate/Container/Container.php(804):Illuminate \ Container \ Container-&gt; resolveClass(Object(ReflectionParameter))#5 / Applications / AMPPS / www / loan / vendor / l in / Applica第734行/ tPS / www / loan / vendor / laravel / framework / src / Illuminate / Container / Container.php

我该如何解决这个问题?因为我尝试的一切都不起作用。 提前谢谢!

3 个答案:

答案 0 :(得分:1)

简短回答:是的。将其添加到文件顶部:

use Illuminate\Support\Facades\Config;

答案 1 :(得分:1)

你需要将这个逻辑移到你的ViewServiceProvider而不是试图直接在配置文件中执行此操作,这是一个很大的禁忌。

所以我们要做的是

php artisan make:provider MyViewServiceProvider

这将导致文件存在于:

App\Providers\MyViewServiceProvider

现在我们要打开config/app.php。找到此文件中的现有ViewServiceProvider::class,并将其替换为上面的命名空间路径。看起来应该是这样的:

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

现在在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);
    });
}

走这条路线将确保首先观察您的路径。如果在该路径中找不到该视图,则可以回退到Laravel的默认视图路径。

修改

请注意,您的类需要扩展默认的ViewServiceProvider,并且必须声明其他2个函数,整个文件应如下所示:

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

答案 2 :(得分:0)

您可以在应用的配置文件中使用config - 和request - 助手。

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