Laravel 5的动态路径 [英] Dynamic paths for laravel 5
问题描述
为Laravel 5构建多租户包时,我不得不找出如何制作路径和名称空间是动态的.
While building multi-tenancy packages for Laravel 5 I had to find out how to make the paths and namespaces dynamic.
这将涉及:
- 意见;动态添加在根名称空间中可用的模板目录
- lang;动态添加在根名称空间中可用的语言目录
- 路线;动态添加路由文件
- 配置;从动态位置合并其他配置文件 供应商允许从动态位置获得自定义供应商和软件包
- views; adding a template directory dynamically that is available in the root namespace
- lang; adding a language directory dynamically that is available in the root namespace
- routes; adding a routes file dynamically
- config; merging additional configuration files from a dynamic location
- vendor; allowing custom vendors and packages to be available from a dynamic location
推荐答案
视图
使用服务提供商,您可以在boot()
方法中使用以下内容来查看可以在根名称空间(view('your-view')
而不是view('package::your-view')
)中使用:
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
使用服务提供商,您可以在boot()
方法中使用以下内容,其中
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;
});
路线
到目前为止,路线是最简单的.只需使用require_once
或使用Laravel方法添加它们即可:\File::requireOnce()
.
routes
Routes is by far the easiest. Simply include them using a require_once
or by using the Laravel method: \File::requireOnce()
.
我使用了一个目录,该目录将允许租户否决核心配置.请建议这里没有安全性或健全性检查,因此访问应受到限制.
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.
使用服务提供商,您可以在boot()
方法中使用以下内容
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.
使用动态加载的类的可能性真的很有趣.为此,您需要在服务提供商ClassLoader addDirectories()
方法>
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']);
其他注意事项
以上代码可以使用服务提供商来实现.为了使服务器提供程序正常工作,您必须将它们添加到providers
数组下的config/app.php
文件中.不这样做将不会启用服务提供商中的任何代码.
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.
这篇关于Laravel 5的动态路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!