dsvs50005 2016-08-15 08:18
浏览 49
已采纳

Twig with MVC - 如何在运行操作文件后获取变量

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

  • 写回答

1条回答 默认 最新

  • doushaju4901 2016-08-15 20:20
    关注

    Okay, the one of possible method how to get it is prepare url with 'GET' params in header() function.

    So, modified class Insert is like below (only a matter lines I've attached):

    $validate   = new Validation;
    
    // phoneValidate for form validation
    if (!$validate->phoneNum($phoneNum)) {
        $headerUrlParam .= $validate->returnUrlParam($headerUrlParam) . "validatePhoneNum=error";
    }
    
    // url forf validation
    if (!$validate->url($url)) {
        $headerUrlParam .= $validate->returnUrlParam($headerUrlParam) . "validateUrl=error";
    }
    
    header("Location: /codeme/06_blog_v5_mvc/index.php" . $headerUrlParam);
    }
    

    The method returnUrlParam() in $validate object is used just for prepare the right sign '?' or '&' because when url ends on index.php we need to use the '?' sign and if there is already some 'GET' values, we need to use the '&' sign.

    When user will fill the phoneNum field properly and the url field not correctly I want to return in URL only this one wrong value.

    Sample#1: when url is wrong and phoneNum is correct: index.php?validateUrl=error Sample#2: when url and phoneNum is wrong: index.php?validatePhoneNum=error&validateUrl=error

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析