我在连接到Cpanel上相同数据库的同一服务器上有两个laravel项目,第一个项目是主项目,另一个是子域项目。我想从主项目的images文件夹中查看子域项目的图像。我试图返回目录,但没有成功
<img src="{{ url('../images/'.$item->image)}}">
但是,当我将子域项目中的图像存储到主项目中的images文件夹中时,它运行良好。但我无法将它们找回视图。
好的-这就是您可以做的工作。
如果还没有,请创建自己的帮助程序文件,并将其添加到composer的自动加载中:
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') }}">
希望这可以帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句