简体   繁体   中英

How to save data on server without using database?

I have an application developed with Laravel. My software has settings that are used globally and should be available in all controllers (such as default information). I take this information from the database in the main controller every time a request is sent and save it in a variable.

namespace App\Http\Controllers;

class Controller extends BaseController
{

    protected $config;

    public function __construct()
    {
            $this->config= DB::table('config')->get();
    }


}

Is there a way to save and use this information without the intervention of a database? I don't want to use sessions.

It is better if a solution is introduced using laravel packages.

Thanks

Assuming that you collection doesn't hold a lot of data, you can always put it inside your custom config . Create a php file inside your app/config directory, where you can put all your values like this:

<?php

return [
    'key1' => value1,
    'key2' => value2,
];

You can create any data structure here that you might need. Now, when you need to read single key from this data, you can use Laravel's config() helper:

$config = config('config_name.key');

If you want to get whole collection of the data, you can do it with the same config() helper, like this:

$config = config('app.config_name'); 

Hope that I understood your question right, and that this can lead you in right direction. You can read more about config on official documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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