Skip to content

Instantly share code, notes, and snippets.

@fuale
Last active May 31, 2018 18:02
Show Gist options
  • Save fuale/abe5aa01378e7e6b82b650a6803e5d0b to your computer and use it in GitHub Desktop.
Save fuale/abe5aa01378e7e6b82b650a6803e5d0b to your computer and use it in GitHub Desktop.
Admin panel - create controller
<?php
/**
* Добавление в конфиг запись
*/
public function newRoute($POST)
{
$replace = "$1$2
\"{$POST['url']}\" => [
\t\"controller\" => \"{$POST['controller']}\",
\t\"action\" => \"{$POST['action']}\",
],
//do_not_erase";
$file = ROOT . '/app/config/config.php';
$pattern = '/(const ROUTES = \[)([\W\w]+)(\/\/do_not_erase)/';
file_put_contents($file, preg_replace($pattern, $replace, file_get_contents($file)));
return true;
}
/**
* Создание файла
*/
public function createFiles($data)
{
$folder = ucfirst($data['controller']);
$file = ucfirst($data['action'] . 'Controller.php');
$finalName = ROOT . "/app/controllers/{$folder}/{$file}";
if (!file_exists($finalName)) {
if (!mkdir(ROOT . "/app/controllers/{$folder}")) {
$this->error = "Ошибка создания директории";
}
$defaultPath = ROOT . '/app/storage/extensions/default/controller.php';
$default = file_get_contents($defaultPath);
$controller = ucfirst($data['action']);
$action = lcfirst($data['action']);
$pattern = '/([\W\w]+)(%)([\W\w]+)(%)([\W\w]+)/';
$finalData = preg_replace($pattern, '$1' . $controller . '$3' . $action . '$5', $default);
file_put_contents($finalName, $finalData, FILE_APPEND);
if ($this->newRoute($data)) {
return true;
}
} else {
$this->error = "Контроллер уже существует, измените имя action.";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment