Dynamic paths for laravel 5

余生长醉 提交于 2019-12-04 18:34:36

views

Using a service provider you can use the following in your boot() method for views to be available in the root namespace (view('your-view') instead of view('package::your-view')):

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

lang

Using a service provider you can use the following in your boot() method where $path is the new path for your 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 is by far the easiest. Simply include them using a require_once or by using the Laravel method: \File::requireOnce().

config

I used a directory that would allow a tenant to overrule core configs. Please advice there are no security nor sanity checks here so access should be limited.

Using a service provider you can use the following in your boot() method

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

This will merge existing configurations by overruling their values with the provided config files.

vendor

Really interesting is the possibility to use dynamically loaded classes. In order to do this you will need to use the ClassLoader addDirectories() method in your service provider

\Illuminate\Support\ClassLoader::addDirectories(['/path/to/vendors']);

additional considerations

The above code can be implemented using service providers. In order for a server provider to work you must add them to the config/app.php file under the providers array. Not doing so will not enable any of the code in the service provider.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!