in one of my php
file content have this below codes:
<?php
return [
// ...
'providers' => [
Illuminate\Auth\AuthServiceProvider::class,
// ...
],
'aliases' => [
// ...
'Form' => Collective\Html\FormFacade::class,
'Html' => Collective\Html\HtmlFacade::class,
],
];
here i want to know how can i add some item to providers
array and save it the same path with file name?
for exapmle i want to add Hekmatinasser\Verta\VertaServiceProvider::class
into array and array should be:
<?php
return [
// ...
'providers' => [
Illuminate\Auth\AuthServiceProvider::class,
Hekmatinasser\Verta\VertaServiceProvider::class
// ...
],
//...
];
can we add and save it into file?
This is usually bad, and changing a config file like this, is not something i would do. There must be a better solution if you told us why you want to change the file. But, a quick hack would look something like this.
Instead of regex, i used a different approach, we can import the config array into a variable, loop over every item, and print it with the specific syntax, and some basic formatting.
$filename = "config.php";
$config = include $filename;
// Change array here, like this
$config['providers'][] = 'Hekmatinasser\Verta\VertaServiceProvider';
$string = "<?php\n\nreturn [\n";
foreach ($config as $key => $value) {
$string .= "\n\t'$key' => [\n";
if (isset($value[0])) {
// Numeric array
foreach ($value as $numeric_value) {
$string .= "\t\t$numeric_value::class,\n";
}
} else {
// Associative array
foreach ($value as $assoc_key => $assoc_value) {
$string .= "\t\t'$assoc_key' => $assoc_value::class,\n";
}
}
$string .= "\t\t\n],";
}
$string .= "\n\n];";
$file = fopen($filename, "w") or die("Unable to open file!");
fwrite($file, $string);
fclose($file);
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.