【问题标题】:my zend session name spacing does not work我的 zend 会话名称间距不起作用
【发布时间】:2013-06-11 08:33:38
【问题描述】:

我是 Zend 的新手,非常渴望学习,所以我非常感谢一些帮助和指导。

我正在尝试创建一个“类中的方法”,它将成员访问的产品页面的会话变量保存到站点,即

i,e examplesite com/product/?producttype= 6

我想将数字 6 保存在会话变量中。我也不想为整个站点创建一个全局会话;我只希望它用于选定的页面。所以,我想我必须在所选页面上有 Zend_Session::start() ;但我不清楚这应该怎么做。

我应该在页面视图页面中实例化它吗?即产品页面或在产品页面的 indexAction() 方法中执行此操作。我试图在下面实例化它,但它不起作用。

public function rememberLastProductSearched()

{          //my attempt to start a session start for this particular page.
      Zend_Session::start();

}

$session->productSearchCategory = $this->_request->getParam('product-search-category');
    return"  $session->productSearchCategory   ";
   }

else
{ 
  //echo " nothing there
 return "  $session->productSearchCategory";
 //"; 

}

}

使用 rememberLastProductSearched() 方法,我试图让该方法首先检查用户是否搜索过新产品或默认情况下刚刚到达该页面。即他是否使用 get() 操作来搜索新产品。如果答案是否定的,那么我希望系统检查它们是否是以前保存的会话变量。所以在程序语法中它应该是这样的:

if(isset($_Get['producttype']))
 {
   //$dbc database connection
$producttype = mysqli_real_escape_string($dbc,trim($_GET['producttype']));

 }
  else
  if(isset($_SESSION['producttype'])){

   $producttype =   mysqli_real_escape_string($dbc,trim($_SESSION['producttype']));       

}

你能帮我了解一下 Zend/oop 语法吗?我完全不知道应该怎么做?

【问题讨论】:

    标签: oop zend-framework zend-session-namespace


    【解决方案1】:

    你在询问一个动作中的简单工作流程,它应该以这样的开头:

    //in any controller
    public function anyAction() 
    {
        //open seesion, start will be called if needed
        $session  = new Zend_Session_Namespace('products');
        //get value
        $productCategory = $this->getRequest()->getParam('producttype');
        //save value to namespace
        $session->productType = $productCategory;
        //...
    }
    

    现在要将其移至单独的方法,您必须将数据传递给该方法...

    protected function rememberLastProductSearched($productType)
    {
        //open seesion, start will be called if needed
        $session  = new Zend_Session_Namespace('products');
    
        $session->productType = $productType;
    }
    

    所以现在如果你想测试一个值的存在......

     //in any controller
        public function anyAction() 
        {
            //open seesion, call the namespace whenever you need to access it
            $session  = new Zend_Session_Namespace('products');
    
            if (!isset($session->productType)) {
                $productCategory = $this->getRequest()->getParam('producttype');
                //save value to session
                $this->rememberLastProductSearched($productCategory)
            } else {
                $productCategory = $session->productType;
            }
        }
    

    就是这样。

    请注意您的工作流程,因为有时很容易无意中覆盖您的会话值。

    【讨论】:

      【解决方案2】:
      $session = new Zend_Session_Namespace("productSearch");
      if ($this->getRequest()->getParam('producttype')) { //isset GET param ?
          $session->productType = $this->getRequest()->getParam('producttype');
          $searchedProductType = $session->productType;
      } else { //take the session saved value
          if ($session->productType) {
             $searchedProductType = $session->productType;
           }  
      }
      //now use $searchedProductType for your query
      

      【讨论】:

      • 是的。我意识到那部分。但问题是我想首先检查用户是否进行了新搜索。即新的获取产品。如果没有,那么我将恢复到旧的会话信息。但是如果添加了新产品,那么我需要删除旧的会话变量并添加新信息。我要找的东西清楚吗?
      • 喜康拉德www和罗克福德。我现在将尝试代码。谢谢你们俩。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-12
      • 2019-02-26
      相关资源
      最近更新 更多