【问题标题】:Sending a variable to the view which is used in controller with phalconPHP使用 phalconPHP 将变量发送到控制器中使用的视图
【发布时间】:2014-06-24 09:59:59
【问题描述】:

我有一个小问题,我在控制器中声明了一个布尔类型的变量。

$done=False

现在控制器中有一个触发器可以将其变为 True,此时我想使用此控制器将其发送到相应的视图..我使用了以下内容。

$done=True;
$this->view->setVar("done", $done);

现在,当我尝试在相应的视图中调用它时,它对这个变量一无所知。

if($done==True)
{
   echo'
   <div class="alert alert-success">
       <a href="addNewSkill" class="alert-link">Add Another Here!</a>
    </div>
     ';
 }

它给了我以下信息:

注意:未定义变量:在 >C:\xampp\htdocs\Blueware\app\views\addNewSkill\index.phtml 第 36 行完成

现在有没有更好的方法将此变量发送到视图,还是我犯了一个错误?

完整的控制器/动作代码:

<?php
        class addNewSkillController extends \Phalcon\Mvc\Controller{

            public function indexAction(){

            }

            public function confirmAction(){

               $this->view->disable();
               $done=False;

                if($this->request->isPost()) {
                    $dataSent = $this->request->getPost();

                    $skill = new Skills();
                    $skill->technology      = $dataSent["technology"];
                    $skill->skillName   = $dataSent["skillName"];
                    $skill->description  = $dataSent["description"];
                    $savedSuccessfully = $skill->save();

                    if($savedSuccessfully) {
                        $done=True;
                        $this->view->setVar("done", $done);
                    } else {
                        $messages = $skill->getMessages();

                        echo "Sorry, the following problems were generated: ";
                        foreach ($messages as $message) {
                            echo "$message <br/>";
                        }
                    }
                } else {
                    echo "The request method should be POST!";  
                }



            }


        }

完整查看代码:

<?php
if($done==True)
{
  echo'
  <div class="alert alert-success">
    <a href="addNewSkill" class="alert-link">Add Another Here!</a>
  </div>
  ';
}


?>

【问题讨论】:

  • 在确认操作中禁用查看。

标签: php html phalcon


【解决方案1】:
   if($savedSuccessfully) {
        $done=True;
        $this->view->setVar("done", $done);
    } else {

你应该在那里设置变量,但是在视图中设置似乎它没有保存,因此没有传递变量

类似

   if($savedSuccessfully) {
        $done=True;
    } else {
    ...later in code ...

   $this->view->setVar("done", $done);

甚至只是

   $this->view->setVar("done", $savedSuccessfully);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-11
    • 2021-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-02
    相关资源
    最近更新 更多