@keborg

Как управлять конфигом через сайт?

В коде я хотел сделать функцию, которая может поменять значение файла config.php через админ-панель
код:
<?php



/*  СОЗДАТЬ КЛАСС (С ФУНЦИЕЙ)/ФУНКЦИЮ ДЛЯ ПЕРЕЗАПИСЫВАНИЯ ЗНАЧЕНИЙ */


class ReplConfig {
public $config = [
    // Главная страница
    "index" => ["news" => "Новости"],

    // Header
    "header" => ["main" => "Главная", "contacts" => "Контакты", "services" => "Услуги", "blog" => "Блог", "promo" => "Промо", "about" => "О нас", "cart" => "Корзина"],

    // Контакты
    "" => "",

    // Услуги
    "" => "",

    // Блог
    "" => "",

    // Промо
    "" => "",

    // О нас
    "" => "",

    // Корзина
    "" => "",

    "" => "",
    "" => "",

  ];
public $news1 = $this->$config["index"]["news"];
public $main1 = $this->$config["header"]["main"];
public $contacts1 = $this->$config["header"]["contacts"];
public $services1 = $this->$config["header"]["services"];
public $blog1 = $this->$config["header"]["blog"];
public $promo1 = $this->$config["header"]["promo"];
public $about1 = $this->$config["header"]["about"];
public $cart1 = $this->$config["header"]["cart"];


public function replConfig($news=$news1, $main=$main1, $contacts=$contacts1, $services=$services1,$blog=$blog1, $promo=$promo1, $about=$about1,$cart=$cart1){
  $this->$config["index"]["news"] = $news;
  $this->$config["header"]["main"] = $main;
  $this->$config["header"]["contacts"] = $contacts;
  $this->$config["header"]["services"] = $services;
  $this->$config["header"]["blog"] = $blog;
  $this->$config["header"]["promo"] = $promo;
  $this->$config["header"]["about"] = $about;
  $this->$config["header"]["cart"] = $cart;
}



}
?>

Выдаёт ошибку:
Fatal error: Constant expression contains invalid operations in C:\OpenServer\domains\everest.ru\php\config.php on line 38

Я знаю что могу просто сделать с помощью file_get_contents/file_put_contents но я не думаю что это хорошая идея
(сразу говорю что коммы делал для себя)
  • Вопрос задан
  • 112 просмотров
Решения вопроса 1
Compolomus
@Compolomus Куратор тега PHP
Комполом-быдлокодер
public function loadConfig(string $path) {
    if (!file_exists($path)) {
        throw new InvalidArgumentException('bad path');
    }
    $this->config = include($path);
}

public function saveConfig(string $path) { // test/1.cnf
    file_put_contents($path,  "<?php\n\n" . 'return ' . var_export($this->config, true) . ";\n");
}

Да и вообще кидать путь к конфигу в конструктор, а конфиг по умолчанию можно удалить
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы