简体   繁体   English

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

[英]Is It Possible to use Laravel 4 without a Default Database

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. 基本上,用户选择记录和类型,应用程序将根据他们的选择生成.pdf文件。 I have multiple connections defined in app/config/database.php but I don't want to connect to any of them by default. 我在app/config/database.php定义了多个连接,但我不想默认连接到任何连接。 Is there a way to tell Laravel not to connect to a database? 有没有办法告诉Laravel 不要连接数据库? I've tried a few things (all in app/config/database.php ), first being: 我尝试了一些东西(全部在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. 这感觉就像一个解决方案,而不是一个实际的解决方案。

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数据库连接。

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

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. 只要你没有在默认的sqlite配置部分中更改任何内容,所有都应该可以工作。 You don't necessarily have to use the database, but it will alleviate those errors you received. 您不一定要使用数据库,但它会减轻您收到的错误。

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. 另外,如果尚未生成'production.sqlite'文件,请确保该文件存在。 You can do this easily from the command line: 您可以从命令行轻松完成此操作:

touch app/database/production.sqlite

暂无
暂无

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

相关问题 将 Laravel 与微服务一起使用:是否可以在没有数据库的情况下使用 eloquent? - Using Laravel with microservices: Is it possible to use eloquent without a database? 是否可以在没有 Laravel 的情况下使用 livewire - Is it possible to use livewire without Laravel 是否可以将 Laravel Facades 与 Illuminate Database Standalone 一起使用? - Is it possible to use Laravel Facades with Illuminate Database Standalone? 在Laravel中可以使用mysqli作为数据库驱动程序吗? - Is it possible to use mysqli as database driver in Laravel? 是否可以在没有静态的laravel路线中使用模型? - Is it possible to use models in laravel routes without static? 有没有办法在没有数据库的情况下使用laravel 4.2身份验证? - Is there a way to use laravel 4.2 authentication without a database? Laravel 5 - 是否可以在不使用Eloquent或Artisan的情况下使用MySQL数据库? - Laravel 5 - Is it possible to work with a MySQL database without using Eloquent or Artisan? Laravel 4-尝试在表单中使用默认内容和不包含默认内容的``创建''布局 - Laravel 4 - trying to use 'create' layout with and without default content in the form 是否可以返回withwithinput这样的数据库数据并将其与{{old()}} laravel一起使用? - is it possible to return database data like withinput and use it with {{old()}} laravel? 如何在没有任何外部数据库的情况下将 mysql 与 laravel 一起使用 - how to use mysql with laravel without any external database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM