4

I am looking to set up one instance of laravel and run multiple apps. Each app would have it's own namespace with custom routes,config, assets, views and its owncomposer.json. All apps will share the same vendor file. This is what I have done so far. Created multiple namespaces, and that's how each app directory structure look like

 |--app
    |--App1
       |-- app
       |-- Bootstrap
       |-- config
       |-- database 
       |-- public
       |-- resources
       |-- storage
       |-- ..  
       |-- composer.json
    |-- App2
       |--..
 |--vendor

to resolve the vendor dependencies I change the vendor folder location in composer.json likes this

"config": {
    "preferred-install": "dist",
    "vendor-dir":"../vendor"
}

I also updated the vendor path in autoload.php to point to the correct location.

The env where I am setting up this project, also runs other laravel projects. So all the ingredients are there(htaccess,, vhosts, ....).

When I ran composer dump-autoload, everything loads I get no errors. When I navigate to my route, I get a blank page. App debug is set to true.

Is my approach to set up laravel in this fashion correct? if so where did I go wrong or what am I doing wrong.
Thanks a lot in advance.

After I got a little hint from the comment below, I was able to make the set up works. 1 - Make sure storage permission is set to 775 writeable. 2 - Make sure you have created .env file. In my case had to create it, did transfer over.

my vhosts

<VirtualHost *:80>
    ServerName app1.server.local
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/tenants/app1/public
    <Directory /var/www/tenants/app1/public/>
       Options Indexes FollowSymLinks MultiViews
       AllowOverride All
       Order allow,deny
       allow from all
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>


<VirtualHost *:80>
    ServerName app2.server.local
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/tenants/app2/public
    <Directory /var/www/tenants/app2/public/>
       Options Indexes FollowSymLinks MultiViews
       AllowOverride All
       Order allow,deny
       allow from all
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Hope this help anyone looking to set up one laravel instance and run multiple apps.

mdamia
  • 4,447
  • 1
  • 24
  • 23
  • 1
    Is your storage folder in `app/storage` writable? Do you get any errors in your network tab? (F12->Network, FireFox or Chrome) – Tim Lewis Sep 02 '15 at 17:43
  • @TimLewis Man, I don't know how I missed that. I changed storage writing permissions 775 and I was also missing .env file, had to create it. It works like a charm. Thanks – mdamia Sep 02 '15 at 17:56
  • No problem. I've missed that step far too many times myself, but I'm glad you got it working. Cheers! – Tim Lewis Sep 02 '15 at 18:09

2 Answers2

0

Tim's comment pointed out what I was missing. I changed the storage folder permission to 775. I added .env file. and Booya, I have multiple apps with a single laravel instance. It works like a charm.

mdamia
  • 4,447
  • 1
  • 24
  • 23
  • Until stuff needs to get updated, that is. Composer isn't meant to manage central dependendies, it is meant to do the opposite, because central installations always trigger the pain of not being able to individually update dependencies. – Sven Sep 02 '15 at 20:57
  • @Sven I am a bit confused though. I added and updated dependencies on each project individually without a glitch. [Composer](http://www.slideshare.net/jasongr/composer-23263197). The slide shows states that composer manages central dependencies. – mdamia Sep 02 '15 at 22:01
  • Example: App1 was last updated to Laravel 5.1.2 - the lock file would ensure it will be this version. Now you see an important bug fix, and run update within App2. This will take this app to Laravel 5.1.6, and your shared vendor folder also affects App1. You then deploy to the production server: First App2, then App1, and always run composer install for each app in this order. Did you get the important bug fIx deployed? No. – Sven Sep 03 '15 at 19:18
  • The slide show clearly states: Composer manages dependencies PER PROJECT. If your several apps share a common vendor folder, they are ONE project and must have only one composer.json file. If not: no shared vendor folder. – Sven Sep 03 '15 at 19:24
  • @Sven, Thanks for the explanation. If I ran composer update for each app to ensue that composer.lock is up to date. Would not that ensure that all apps have the same version? – mdamia Sep 04 '15 at 16:06
  • Yes, but why running update multiple times at the same time (which cannot be done, one update will always be later than the other), why not having one single `composer.json` and admit that it's one single multi-app project? Because that is what it is. – Sven Sep 05 '15 at 13:24
0

For Laravel 5, 6 and 7+

After adding the new vendor folder config:

...
"config": {
    ...,
    "vendor-dir": "../vendor"
},
...

Then run composer update

Then you need to change two files:

  1. For your app: public/index.php

    require __DIR__.'/../../vendor/autoload.php';

  2. Your artisan command in the root folder: artisan

    require __DIR__.'/../vendor/autoload.php';

  3. Package auto-discovery in Illuminate\Foundation\PackageManifest:

    $this->vendorPath = $basePath.'/../vendor'; //Change this line in constructor

and rerun

php artisan package:discover --ansi
BassMHL
  • 8,523
  • 9
  • 50
  • 67