简体   繁体   English

Laravel 5-根据域设置配置变量

[英]Laravel 5 - Set config variable based on domain

I need to check what the current domain is, and set a config variable (or other?) based on that. 我需要检查当前域是什么,并基于此设置配置变量(或其他?)。 It can then be used in controllers. 然后可以在控制器中使用它。 models or views. 模型或视图。 The idea is to build one "template" website with one DB, but the data delivered will depend on the domain used. 这个想法是用一个数据库建立一个“模板”网站,但是传递的数据将取决于使用的域。

I'm thinking about doing it in a middleware or service provider (I am new to Laravel 5). 我正在考虑在中间件或服务提供商中做到这一点(我是Laravel 5的新手)。

What would be the best way of doing it? 最好的方法是什么? Any suggestions/advices are appreciated :) 任何建议/意见,表示赞赏:)

That's what I ended up doing: 1 - detect the domain in App\\Providers\\ConfigServiceProvider and add it to the config: 这就是我最终要做的事情:1-在App \\ Providers \\ ConfigServiceProvider中检测域并将其添加到配置中:

public function register() {
    switch (Request::server("HTTP_HOST")) {
        case 'domain1.com':
            $country = 'AA';
            break;
        case 'domain2.com':
            $country = 'BB';
            break;
        default:
            $country = 'CC';
            break;
    }
    $config = app('config');
    $config->set('country', $country);
}

2 - make it available in all controllers and models by adding this to the base controller and model classes (if you have any): 2-通过将其添加到基本控制器和模型类(如果有)中,使其在所有控制器和模型中均可用:

abstract class Controller extends BaseController {
    use DispatchesCommands,
        ValidatesRequests;

    function __construct() {
        $this->country = config('country');
    }
}

3 - In my case it is useful to create a global scope and a trait to register it in the models that need to filter all queries by country. 3-在我的情况下,创建全局范围和特征以将其注册到需要按国家/地区过滤所有查询的模型中很有用。 I've added them into a sub-directory of Models: 我已经将它们添加到Models的子目录中:

Scope 范围

use Illuminate\Database\Eloquent\ScopeInterface;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class CountryScope implements ScopeInterface {

    /**
     * Apply scope on the query.
     *
     * @param  \Illuminate\Database\Eloquent\Builder  $builder
     * @param  \Illuminate\Database\Eloquent\Model  $model
     * @return void
     */
    public function apply(Builder $builder, Model $model) {
        //$builder->whereNull($model->getQualifiedDeletedAtColumn());
        $column = $model->getCountryIDColumn();
        $builder->where($column, $model->country);
    }

    /**
     * Remove scope from the query.
     *
     * @param  \Illuminate\Database\Eloquent\Builder  $builder
     * @param  \Illuminate\Database\Eloquent\Model  $model
     * @return void
     */
    public function remove(Builder $builder, Model $model) {

        $query = $builder->getQuery();

        $column = $model->getCountryIDColumn();

        foreach ((array) $query->wheres as $key => $where) 
        {
            if ($where['column'] == $column) {

                unset($query->wheres[$key]);

                $query->wheres = array_values($query->wheres);
            }
        }
    }

}

Trait 特征

trait CountryTrait {

    /**
     * Boot the country trait for a model.
     *
     * @return void
     */
    public static function bootCountryTrait()
    {
        static::addGlobalScope(new CountryScope);
    }

    /**
     * Get the country id column name for applying the scope.
     * 
     * @return string
     */
    public function getCountryIDColumn()
    {
        return $this->getTable().'.'.'country_id';
        //return 'country_id';
    }
}

And in each model that need it 在每个需要它的模型中

class Project extends Model {
    use Traits\CountryTrait;
    ...
}

Please post a new answer if you know of a better way to do this, or comment if you have suggestions for improvement. 如果您知道执行此操作的更好方法,请发布新答案;如果您有改进的建议,请发表评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM