Dynamic paths for laravel 5

While creating packages with multiple tenants for Laravel 5, I had to figure out how to make paths and namespaces dynamic.

This will include:

  • view; add a dynamic template directory available in the root namespace
  • languages; adding a dynamic language directory available in the root namespace
  • routes add route file dynamically
  • configurations; merging additional configuration files from a dynamic location
  • Provider allowing custom vendors and packages to be accessible from a dynamic location
+1
source share
1 answer

view

, boot(), (view('your-view') view('package::your-view')):

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

, boot(), $path -

$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;
});

, , . require_once Laravel: \File::requireOnce().

, . , , , .

, boot()

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

, .

. ClassLoader addDirectories()

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

. , config/app.php providers. , - .

+4

Source: https://habr.com/ru/post/1598075/


All Articles