简体   繁体   中英

Best practices for read array from file in Laravel

My question might be stupid. But i need to clear my concept about it.

There are several ways to read array in Laravel. like config() variable , .env function, trans() function, file read like .csv , .txt , .json etc.

May be all of them are different purpose. But i need to know what will be the good practice to read array data from my controller. An example given. Thanks

Example array:

       [
        "mohammad" => [
           "physics" => 35,
           "maths" => 30,   
           "chemistry" => 39
        ],

        "qadir" => [
           "physics" => 30,
           "maths" => 32,
           "chemistry" => 29
        ],

        "zara" => [
           "physics" => 31,
           "maths" => 22,
           "chemistry" => 39
        ]
     ]

Laravel uses var_export() under the hood to cache the config in this way:

$config = [
    'myvalue' => 123, 
    'mysub' => [
         'mysubvalue' => true
    ]
];
$code = '<?php return '.var_export($config, true).';'.PHP_EOL;

where $config can be a multidimensional associative array. if you put that string into a file:

file_put_contents(config_path('myconf.php'), $code);

in the code you have to simply include that file to have your structure

$myconfig = require config_path('myconf.php');
dd($myconfig);

or (if is config file) call

echo config('myconf.myvalue');

To retrive values in Laravel style you can use the Illuminate\\Config\\Repository class eg.

$conf = new Illuminate\Config\Repository($myconfig);
echo $conf->get('mysub.mysubvalue');

or

echo Illuminate\Support\Arr::get($myconfig, 'mysub.mysubvalue');

hope this will clarify and help

There are serialize() and unserialize() functions that creates/loads a textual representation of any php value.

However, I would use files for storing data ONLY if the data do not change much on runtime. Eg for caching or configuration purposes. Otherwise, you may run into collisions when multiple sessions attempt to read/write the file at the same time and produce weird errors

http://php.net/manual/en/function.serialize.php

Responses to some of the comments by OP:

Laravel uses config files that are interpreted, ie parsed by the PHP once the framework boots up. Use of such files enable the use of some neat language features, such as class injection, allows to version the config files, and makes life somewhat easier for devs as the config pertaining framework stuff is stored in the code.

For runtime, session-specific stuff, use a database. There is $_SESSION[] variable for storing temporary data. Depending on your config the values could be stored in memory or files, and the PHP takes care of them.

Just a minor correction to the answer above: the author asked about reading the data, so they presumably need unserialize(file_get_contents('data.file')); However, I do support the answer above as it's really bad idea to store and read something from the filesystem, not only because of concurrent read/writes , but because of speed/file access/caching issues as well.

I don't know if this is best practice, but i can sure you that it's working, not just in Laravel but in any other PHP project.

That been said, to read an array from a file all you have to do is to include this file, the returned array you can assign it to a variable.

The array must be returned form the included file, it's important

Example:

path/to/my/array_file.php

<?php

return [

    'resource' => [

        'delete'  => 'Are you sure you want to delete this resource?',
        'updated' => 'Data for this resource has been successfully updated',
        'created' => 'Data for this resource has been successfully created',
        'deleted' => 'Data for this resource has been successfully deleted',
    ],
];

If i need to access this array any where in my project, i can include it like this:

$messages = include('path/to/my/array_file.php');

Now $messages is just another php array.

if you var_dump($messages) or dd($messages) in Laravel you get something like this:

array:2 [▼
  "resource" => array:4 [▼
    "delete" => "Are you sure you want to delete this resource?"
    "updated" => "Data for this resource has been successfully updated"
    "created" => "Data for this resource has been successfully created"
    "deleted" => "Data for this resource has been successfully deleted"
  ]
] 

I wanted to import a simple array from a file in a Laravel project . I saved the file in resources/appData and for loading the array I ended up in this:

$categories = include(resource_path('appData/categories.php'));

This will load the array from the file located in 'resources/appData/categories.php' and save it to the $categories variable.

The file returns an array like the config files in Laravel

 <?php
    return [
 
    ];

I don't know if this is a best practice though.

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