Reputation: 161
I have two laravel projects on the same server connected to the same database on the Cpanel, the first project is the main project and the other one is a subdomain project. I want to view images for the subdomain project from the images folder in the main project. I tried to return back to the directory but it didn't work
<img src="{{ url('../images/'.$item->image)}}">
however, when I store images from the subdomain project to the images folder in the main project, it works well. but I can't retrieve them back to the view.
Upvotes: 0
Views: 544
Reputation: 6045
Ok - so here's what you could do to make it work.
If you don't have already, create your own helpers file and add it to composer's autoload:
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"
]
},
//...
}
Register parentUrl
in the container
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);
};
}
Please note the new config entry for app
namespace: parent_asset_url
. You now need to add it to your app.php
config file.
config/app.php
[
//...
'asset_url' => env('ASSET_URL', null),
'parent_asset_url' => env('PARENT_ASSET_URL', null),
//...
]
Lastly you need to add PARENT_ASSET_URL
variable to your .env
file and specify the url of your parent application.
PARENT_ASSET_URL=https://google.com
Recompile autoloader
composer dump-autoload -o
And you can now use the parentAsset
helper to load files directly from the parent domain:
<img src="{{ parentAsset('/assets/images/logo.svg') }}">
Hope this helps.
Upvotes: 1