Dynamic paths for laravel 5 -


while building multi-tenancy packages laravel 5 had find out how make paths , namespaces dynamic.

this involve:

  • views; adding template directory dynamically available in root namespace
  • lang; adding language directory dynamically available in root namespace
  • routes; adding routes file dynamically
  • config; merging additional configuration files dynamic location
  • vendor; allowing custom vendors , packages available dynamic location

views

using service provider can use following in boot() method views available in root namespace (view('your-view') instead of view('package::your-view')):

$this->app['view']->addlocation('/your/new/location'); 

lang

using service provider can use following in boot() method $path new path root namespace translations:

$app->bindshared('translation.loader', function($app) use ($path) {       return new \illuminate\translation\fileloader($app['files'], $path); }); $app->bindshared('translator', function($app) {       $translator = new \illuminate\translation\translator($app['translation.loader'], $app['config']['app.locale']);       $translator->setfallback($app['config']['app.fallback_locale']);       return $translator; }); 

routes

routes far easiest. include them using require_once or using laravel method: \file::requireonce().

config

i used directory allow tenant overrule core configs. please advice there no security nor sanity checks here access should limited.

using service provider can use following in boot() method

 foreach (\file::allfiles('/path/to/configs') $path) {        $key = \file::name($path);        $app['config']->set($key, array_merge(require $path, $app['config']->get($key, [])));  } 

this merge existing configurations overruling values provided config files.

vendor

really interesting possibility use dynamically loaded classes. in order need use classloader adddirectories() method in service provider

\illuminate\support\classloader::adddirectories(['/path/to/vendors']); 

additional considerations

the above code can implemented using service providers. in order server provider work must add them config/app.php file under providers array. not doing not enable of code in service provider.


Comments