本文介绍了如何在Laravel 5中扩展和修改供应商模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Laravel 5应用程序中的软件包 antonioribeiro/firewall ,并且我正在使用数据库存储要阻止的IP地址列表.

I am working with a package antonioribeiro/firewall in my Laravel 5 application and I am using the database to store the list of IP addresses to be blocked.

我已经成功安装了它,并且也能够使用它随附的PragmaRX\Firewall\Vendor\Laravel\Models\Firewall模型.

I have installed it successfully and I am able to use the PragmaRX\Firewall\Vendor\Laravel\Models\Firewall model that it comes with too.

我遇到的问题是我正在使用多租户数据库,其中基本上每个租户都有自己的数据库,这些租户的模型使用$connection属性指定tenant连接,并且在后台运行根据请求更改租户连接配置.

The problem that I have is that I am working with a multi tenant database where basically every tenant has their own database and the models for these tenants use the $connection property to specify the tenant connection and behind the scenes I change the tenant connection config accordingly to the request.

现在,包装随附的模型如下:

Now the model that comes with package looks like so:

<?php namespace PragmaRX\Firewall\Vendor\Laravel\Models;
/**
 * Part of the Firewall package.
 *
 * NOTICE OF LICENSE
 *
 * Licensed under the 3-clause BSD License.
 *
 * This source file is subject to the 3-clause BSD License that is
 * bundled with this package in the LICENSE file.  It is also available at
 * the following URL: http://www.opensource.org/licenses/BSD-3-Clause
 *
 * @package    Firewall
 * @author     Antonio Carlos Ribeiro @ PragmaRX
 * @license    BSD License (3-clause)
 * @copyright  (c) 2013, PragmaRX
 * @link       http://pragmarx.com
 */

use Illuminate\Database\Eloquent\Model as Eloquent;

class Firewall extends Eloquent {

    protected $table = 'firewall';

    protected $guarded = array();

}

我添加了protected $connection = 'tenant';使其正常运行,但是我正在编辑供应商文件,这些文件不会显示在版本控件中.因此,作为一个新手开发人员,我试图找出如何以某种方式扩展此模型,然后放置$connection属性.

I added protected $connection = 'tenant'; to it to make it work but I am editing vendor files which won't show up in the version control. So being a novice developer I am trying to figure out how to extend this model somehow and then put the $connection property.

我尝试在App命名空间中创建一个新模型,并扩展了该软件包随附的模型,但徒劳无功.

I tried creating a new model in the App namespace and extended the model that comes with the package but in vain.

那么我如何扩展该软件包随附的模型以将其添加到其中,以使其显示在版本控制中,而不是在编辑供应商文件?

So how do I extend the model that comes with the package to add to it so that it shows up in the version control and I am not editing vendor files?

推荐答案

编辑供应商文件是一个不好的想法.如果出于某些原因必须对其进行编辑,我建议分叉该软件包并在分叉的版本中进行编辑,然后更新composer文件以从您自己的分叉中拉出该软件包.但我不建议在这种情况下执行此操作.

Editing vendor files is a bad idea. If you for some reason HAVE to edit them, I would recommend forking the package and edit in the forked version, then update the composer file to pull the package from your own fork instead. But I would not recommend doing this in a case like this.

相反,您应该发布程序包配置文件并更改其应使用的模型.
这是通过在终端中使用artisan vendor:publish命令完成的.
应在config文件夹中创建防火墙软件包的新配置文件.
然后,您需要做的就是创建自己的模型并更改配置文件中的firewall_model属性:

Instead you should publish the package config file and change the model that it should use.
This is done by using the artisan vendor:publish command in the terminal.
A new configuration file for the firewall package should be created in your config folder.
Then all you need to do is create your own model and change the firewall_model property in the config file:

https://github.com/antonioribeiro/firewall/blob/master/src/config/config.php

这篇关于如何在Laravel 5中扩展和修改供应商模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 05:03