从不同的目录检索图像

retrieve images from different directory

我在同一台服务器上有两个 laravel 项目连接到 Cpanel 上的同一个数据库,第一个项目是主项目,另一个是子域项目。 我想从主项目的图像文件夹中查看子域项目的图像。 我尝试 return 返回目录,但没成功

 <img src="{{ url('../images/'.$item->image)}}">

但是,当我将子域项目中的图像存储到主项目中的图像文件夹时,效果很好。但我无法将它们检索回视图。

好的 - 下面是您可以做的事情。

如果您还没有,请创建您自己的助手文件并将其添加到作曲家的自动加载中:

app/helpers.php

<?php

if (! function_exists('parentAsset')) {
    /**
     * Generate a parentAsset path for the application.
     *
     * @param  string  $path
     * @param  bool|null  $secure
     * @return string
     */
    function parentAsset($path, $secure = null)
    {
        return app('parentUrl')->asset($path, $secure);
    }
}

composer.json

{
    //...
    "autoload": {
        "psr-4": {
            "App\": "app/"
        },
        "classmap": [
            "database/seeds",
            "database/factories"
        ],
        "files": [
            "app/helpers.php"
        ]
    },
    //...
}

在容器中注册parentUrl

app/Providers/AppServiceProvider.php

use Illuminate\Routing\UrlGenerator;

public function register()
{
    $this->app->singleton('parentUrl', function ($app) {
        $routes = $app['router']->getRoutes();

        $app->instance('routes', $routes);

        return new UrlGenerator(
            $routes,
            $app->rebinding('request', $this->requestRebinder()),
            $app['config']['app.parent_asset_url']
        );
    });
}

/**
 * Get the URL generator request rebinder.
 *
 * @return \Closure
 */
protected function requestRebinder()
{
    return function ($app, $request) {
        $app['url']->setRequest($request);
    };
}

请注意 app 命名空间的新配置条目:parent_asset_url。您现在需要将它添加到您的 app.php 配置文件中。

config/app.php

[

    //...
    'asset_url' => env('ASSET_URL', null),
    'parent_asset_url' => env('PARENT_ASSET_URL', null),
    //...

]

最后您需要将 PARENT_ASSET_URL 变量添加到您的 .env 文件并指定您的父应用程序的 url。

PARENT_ASSET_URL=https://google.com

重新编译自动加载器

composer dump-autoload -o

您现在可以使用 parentAsset 助手直接从父域加载文件:

<img src="{{ parentAsset('/assets/images/logo.svg') }}">

希望这对您有所帮助。