3

Okay, I am here because I would like to have a site in the following structure:

I would like to use ONE uploads folder to store all of the assets for all 3 languages(plus any additional languages added after), use all of the same templates, and use all of the same css and js files.

My current directory structure is the craft 3 default(I used the cli installer, which is great). Here is the important pieces of what my directory looks like:

-templates
- web
   - dist
      - js
      - css
   - uploads
   - ko
   - pt

Lets say I have the following templates:

- home.html
- newsroom.html
- whitepapers.html
- help.html

I would expect to be able to visit each in the following manner:

(Home)
- https://site.com
- https:///site.com/ko
- https://site.com/pt

(Newsroom)
- https://site.com/newsroom
- https:///site.com/ko/newsroom
- https://site.com/pt/newsroom

...etc hopefully you get the picture.

Is this at all possible with Craft 3? I keep running into issues where each languages site is trying to pull in the uploads or css/js assets from within their respective folders:

enter image description here

Also, here is the sites config in general.php:

'dev' => [
        'siteUrl' => [
          'default' => 'https://site.test',
          'korean' => 'https://site.test/ko',
          'brazil' => 'https://site.test/pt'
        ],
        'devMode' => true,
 ]

SOLUTION For anyone visiting this in the future, here are the updates to my config that solved the problem. Please refer to this after you have read Robin and Jolle's answers, they are both very helpful.

'dev' => [
        'siteUrl' => [
          'default' => 'http://site.test',
          'korean' => 'http://site.test/ko',
          'brazil' => 'http://site.test/bz'
        ],
        'aliases' => [
            'basePath' => '/Users/dzuz/localhost/site/web',
            'baseUrl'  => 'http://site.test/',
        ],
        'devMode' => true,
    ],
Dan Zuzevich
  • 487
  • 2
  • 18

2 Answers2

4

First of all you should remove all "language" subfolders from your web directory because your web server will route directly in those instead of your default web/index.php. Craft will determinate the CurrentSite depending on your url not on the location of the file.

Second: make sure to insert an array with aliases in your config/general.php I'll show a "complex" solution for multiple asset sources for multiple sites. If your site shares the same resources you'll just need the first array element but I want to show the concept

$publicPath = realpath(dirname(__DIR__) . '/web');
// ....
'aliases' => [
    'assetPath1'    => $publicPath . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'folder1' . DIRECTORY_SEPARATOR,
    'assetPath2'    => $publicPath . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'folder2' . DIRECTORY_SEPARATOR,
    //....
]

You should never access those files with

<script src="`{{ siteUrl }}/assets/folder1/jquery.js`">

Because this will use the current site url and append the structure. Therefore you have the alias function

<script src="`{{ alias('assetPath1') }}/assets/folder1/jquery.js`">

Now you can do some fancy stuff like

<script src="`{{ alias('assetPath' ~ currentSite.id) }}/assets/folder1/jquery.js`">

Or whatever identifier you like to access your asset files dynamically.

Robin Schambach
  • 19,713
  • 1
  • 19
  • 44
  • It might be worthwhile to check if this is a completely reliable solution, as it is in opposition with what the Craft Google Hangout video says to do. However, I think the Craft Google Hangout video is not sufficient, and its about time that this be well documented from Craft themselves. – Dan Zuzevich Apr 06 '18 at 12:02
  • Specifically, the Hangout video says to create folders for each language inside of web. However, I dont have time to go back and watch this video again, aka the need for docs. – Dan Zuzevich Apr 06 '18 at 12:03
  • Are you sure they are about Craft 3 and not Craft 2? – Robin Schambach Apr 06 '18 at 13:22
  • Found the video here https://craftcms.stackexchange.com/questions/19601/multi-site-setup-in-craft3 – Dan Zuzevich Apr 06 '18 at 13:22
4

Use aliases, by first defining them in your general.php:

'dev' => [
        'siteUrl' => [
          'default' => 'https://site.test',
          'korean' => 'https://site.test/ko',
          'brazil' => 'https://site.test/pt'
        ],
        'devMode' => true,
        'aliases' => [
            '@baseUrl' => 'http://site.test',
        ],
]

Then in your templates you can now use {{ alias('@baseUrl') }}

<script type="text/javascript" src="{{ alias('@baseUrl') }}/ui/vendor/enquire/enquire.min.js"></script>

For more info, see https://github.com/nystudio107/craft3-multi-environment

Jolle
  • 95
  • 5
  • Thanks man. Robins answer also pointed out the fact that I needed to remove the language folders, which was key to craft not trying to serve files from inside those folders. With the alias piece, and removing those folders, everything works. – Dan Zuzevich Apr 05 '18 at 15:36