是否可以在没有默认数据库的情况下使用Laravel 4

时间:2021-12-24 01:52:31

I'm creating an application that reads information from a number of different databases, but doesn't actually have its own database, as there is no information being written anywhere.

我正在创建一个从许多不同数据库中读取信息的应用程序,但实际上并没有自己的数据库,因为没有任何信息可以写在任何地方。

Basically, a user selects a record and a type, and the application will generate a .pdf file based on their choices. I have multiple connections defined in app/config/database.php but I don't want to connect to any of them by default. Is there a way to tell Laravel not to connect to a database? I've tried a few things (all in app/config/database.php), first being:

基本上,用户选择记录和类型,应用程序将根据他们的选择生成.pdf文件。我在app / config / database.php中定义了多个连接,但我不想默认连接到它们中的任何一个。有没有办法告诉Laravel不要连接到数据库?我尝试了一些东西(都在app / config / database.php中),首先是:

'default' => NULL,
// and 
//'default' => '',

Which both return:

哪两个都回归:

Undefined index: driver

未定义的索引:驱动程序

I've also tried:

我也尝试过:

'default' => 'none',

'connections' => array(
    'none' => array(
        'driver' => '',
        'host' => '',
        ...
     ),
 ),

which in turn returns:

然后返回:

Unsupported driver []

不支持的驱动程序[]

Unsupported host[]

不支持的主机[]

...

...

And lastly setting 'default' => '', which returns:

最后设置'default'=>'',返回:

Database [] not configured.

数据库[]未配置。

I've found ways to use Laravel's models without a database connection, but not actually Laravel itself.

我已经找到了在没有数据库连接的情况下使用Laravel模型的方法,但实际上并没有使用Laravel本身。

Edit:

编辑:

Setting the connection to an existing mysql connection and not selecting a database "works":

设置与现有mysql连接的连接而不选择数据库“工作”:

'default' => array(
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => '',
    'username'  => '****',
    'password'  => '****',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
),

but I'm not sure if that's the right way to do it. It feels like a work-around and not an actual solution.

但我不确定这是否是正确的方法。这感觉就像一个解决方案,而不是一个真正的解决方案。

3 个解决方案

#1


4  

I would do the following:

我会做以下事情:

'driver' => 'sqlite',

and for the sqlite connection settings

并为sqlite连接设置

'sqlite' => [
    'driver' => 'sqlite',
    'database' => ':memory:',
    'prefix' => '',
]

That way, it'll use an in-memory database and the database will cease to exist when the database connection is closed, and since you won't be opening a connection, you'll never have a local database open.

这样,它将使用内存数据库,并且数据库连接关闭时数据库将不再存在,并且由于您不打开连接,因此您将永远不会打开本地数据库。

You could also remove the database service provider from your app.php config file:

您还可以从app.php配置文件中删除数据库服务提供程序:

    'Illuminate\Cookie\CookieServiceProvider',
    //'Illuminate\Database\DatabaseServiceProvider',
    'Illuminate\Encryption\EncryptionServiceProvider',

and the facades for Fluent and Eloquent in the same file:

以及同一文件中的Fluent和Eloquent的外观:

    'Crypt'     => 'Illuminate\Support\Facades\Crypt',
    //'DB'        => 'Illuminate\Support\Facades\DB',
    //'Eloquent'  => 'Illuminate\Database\Eloquent\Model',
    'Event'     => 'Illuminate\Support\Facades\Event',

which will prevent you from being able to connect to local databases and not even boot the sqlite database connection you've just set up.

这将阻止您能够连接到本地数据库,甚至无法启动您刚刚设置的sqlite数据库连接。

#2


0  

What you need to do define the connection in the app/config/database.php file and specify the connection when running a query with laravel's DB::connection('connection_name'). More information here --> http://laravel.com/docs/4.2/database#accessing-connections

您需要做什么来定义app / config / database.php文件中的连接,并在使用laravel的DB :: connection('connection_name')运行查询时指定连接。更多信息 - > http://laravel.com/docs/4.2/database#accessing-connections

#3


0  

Simply define your db driver as 'sqlite' in the database config.

只需在数据库配置中将db驱动程序定义为“sqlite”即可。

'default' => 'sqlite',

As long as you haven't changed anything in the default sqlite config section, all should work. You don't necessarily have to use the database, but it will alleviate those errors you received.

只要您在默认的sqlite配置部分中没有更改任何内容,所有内容都应该有效。您不一定要使用数据库,但它会减轻您收到的错误。

In case you have changed the default sqlite config for whatever reason, you can follow the below steps to configure the sqlite db.

如果您因任何原因更改了默认的sqlite配置,可以按照以下步骤配置sqlite db。

'sqlite' => array(
    'driver'   => 'sqlite',
    'database' => __DIR__.'/../database/production.sqlite',
    'prefix'   => '',
),

Also, make sure this 'production.sqlite' file exists if it doesn't already. You can do this easily from the command line:

此外,请确保此“production.sqlite”文件存在(如果尚未存在)。您可以从命令行轻松完成此操作:

touch app/database/production.sqlite

#1


4  

I would do the following:

我会做以下事情:

'driver' => 'sqlite',

and for the sqlite connection settings

并为sqlite连接设置

'sqlite' => [
    'driver' => 'sqlite',
    'database' => ':memory:',
    'prefix' => '',
]

That way, it'll use an in-memory database and the database will cease to exist when the database connection is closed, and since you won't be opening a connection, you'll never have a local database open.

这样,它将使用内存数据库,并且数据库连接关闭时数据库将不再存在,并且由于您不打开连接,因此您将永远不会打开本地数据库。

You could also remove the database service provider from your app.php config file:

您还可以从app.php配置文件中删除数据库服务提供程序:

    'Illuminate\Cookie\CookieServiceProvider',
    //'Illuminate\Database\DatabaseServiceProvider',
    'Illuminate\Encryption\EncryptionServiceProvider',

and the facades for Fluent and Eloquent in the same file:

以及同一文件中的Fluent和Eloquent的外观:

    'Crypt'     => 'Illuminate\Support\Facades\Crypt',
    //'DB'        => 'Illuminate\Support\Facades\DB',
    //'Eloquent'  => 'Illuminate\Database\Eloquent\Model',
    'Event'     => 'Illuminate\Support\Facades\Event',

which will prevent you from being able to connect to local databases and not even boot the sqlite database connection you've just set up.

这将阻止您能够连接到本地数据库,甚至无法启动您刚刚设置的sqlite数据库连接。

#2


0  

What you need to do define the connection in the app/config/database.php file and specify the connection when running a query with laravel's DB::connection('connection_name'). More information here --> http://laravel.com/docs/4.2/database#accessing-connections

您需要做什么来定义app / config / database.php文件中的连接,并在使用laravel的DB :: connection('connection_name')运行查询时指定连接。更多信息 - > http://laravel.com/docs/4.2/database#accessing-connections

#3


0  

Simply define your db driver as 'sqlite' in the database config.

只需在数据库配置中将db驱动程序定义为“sqlite”即可。

'default' => 'sqlite',

As long as you haven't changed anything in the default sqlite config section, all should work. You don't necessarily have to use the database, but it will alleviate those errors you received.

只要您在默认的sqlite配置部分中没有更改任何内容,所有内容都应该有效。您不一定要使用数据库,但它会减轻您收到的错误。

In case you have changed the default sqlite config for whatever reason, you can follow the below steps to configure the sqlite db.

如果您因任何原因更改了默认的sqlite配置,可以按照以下步骤配置sqlite db。

'sqlite' => array(
    'driver'   => 'sqlite',
    'database' => __DIR__.'/../database/production.sqlite',
    'prefix'   => '',
),

Also, make sure this 'production.sqlite' file exists if it doesn't already. You can do this easily from the command line:

此外,请确保此“production.sqlite”文件存在(如果尚未存在)。您可以从命令行轻松完成此操作:

touch app/database/production.sqlite