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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?