I'm building my PHP project with MVC and Twig and I'm having problem with return one variable string after form validation on action 'input'. Here is my controller file:
// index.php file - here is the begin
require_once './vendor/autoload.php';
//require_once "autoloader.php";
require_once "class/blogcore.php";
require_once "class/pagination.php";
require_once "class/post.php";
require_once "class/validation.php";
$conf = parse_ini_file("config.ini");
$lang = parse_ini_file($conf['LANG_PATH']);
$db = new PDO('mysql:host='.$conf['DB_HOST'].';dbname='.$conf['DB_NAME'].';charset='.$conf['DB_CHARSET'], $conf['DB_USER'], $conf['DB_PASS']);
/*
** This is the controller file
*/
$action = !empty($_GET['action']) ? $_GET['action'] : 'Index';
require_once 'actions/' . $action . '.php';
$action = new $action($db, $lang);
$data = $action->generate();
$template = $action->getTemplate();
if ($template) {
$loader = new Twig_Loader_Filesystem('templates');
$twig = new Twig_Environment($loader);
echo $twig->render($template, $data);
}
Here is my index.php
file which is displaying the index page:
// '/class/index.php' file with class called 'Index'
class Index {
private $lang;
private $db;
public function __construct($db, $lang) {
$this->lang = $lang;
$this->db = $db;
}
public function generate() {
// new Core object
$core = new BlogCore($this->db);
/*
** pagination
*/
$numRows = $core->getNumRows();
$pagination = new Pagination($_GET['p']);
$pagination->setNumRows($numRows);
$pagination->setLimit(3); // set 3 posts on one page
$limit = $pagination->sqlPagination();
$offset = $pagination->sqlPaginationOffset($limit);
/*
** select detalis form database
*/
$posts = $core->select($limit, $offset);
return [
"posts" => $posts,
"pagination" => $pagination,
"lang" => $this->lang
];
}
public function getTemplate() {
return "default.tpl";
}
}
And here is the insert.php
file which is used on action insert
(just when I want do input new data from form to my db). Here I'm checking with regular expression that data filled in fields are correctly. And if not, here I want to return to template my variable which will be displaying notification for user.
// '/class/insert.php' file with 'Insert' class
class Insert {
private $lang;
private $db;
private $postText;
private $fileName;
private $myFile;
private $phoneValidate;
private $urlValidate;
public function __construct($db, $lang) {
$this->lang = $lang;
$this->db = $db;
}
public function generate() {
$postText = $_POST["postText"];
$myFile = $_FILES["myFile"];
$fileName = $myFile["name"];
$phoneNum = $_POST['phoneNumber'];
$url = $_POST['url'];
$validate = new Validation;
$phoneValidate = "test";
// phoneValidate for form validation
if (!$validate->phoneNum(isset($phoneNum))) {
$phoneValidate = "error";
} else {
$phoneValidate = "ok";
}
// new Core object
$core = new BlogCore($this->db);
// do insert in SQL
if (!empty($postText)) {
$core->insert($postText, $fileName);
$core->saveFile($myFile);
}
header("Location: /codeme/06_blog_v5_mvc/index.php");
}
public function getTemplate() {
return null;
}
}
So the question is: how can I modify this file to get my index file (like I'm doing this actually with header()
function and get additional value of variable called $phoneValidate
?
If You need, this project is shared on my BitBucket account:
https://bitbucket.org/Pirum/simple-blog/src